Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 305597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:23:19+00:00 2026-05-12T07:23:19+00:00

When using a ReadWriteLockSlim for multi-threading support, do I need to EnterWriteLock when using

  • 0

When using a ReadWriteLockSlim for multi-threading support, do I need to EnterWriteLock when using AddHandler?

Here are two examples to help illustrate my point:

AddHandler ClassInstance.Event, New EventHandler(AddressOf Me.Method)  

-or-

Me.ReaderWriterLockSlimInstance.EnterWriteLock()
AddHandler ClassInstance.Event, New EventHandler(AddressOf Me.Method)  
Me.ReaderWriterLockSlimInstance.ExitWriteLock()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:23:19+00:00Added an answer on May 12, 2026 at 7:23 am

    It may depend on the event; for example, if you are consuming an event from a dll written in C# via a “field-like event”, i.e.

    public event EventHandler SomeEvent; // synchronized
    

    (yes I know you asked about VB; I explain this below…)

    then this is (per the C# language spec) automatically synchronized, so there are no multi-threading issues (and it helps here that delegates are immutable). However, even in the same library, a non-field-like event might not be synchronized – i.e.

    private EventHandler myField;
    public event EventHandler SomeEvent { // not synchronized
        add { myField += value; }
        remove { myField -= value; }
    }
    

    I know you asked about VB… but you are asking about VB as the comsumer. My point is that it depends on the publisher.

    So if you can’t control the publisher, then manually synchronizing sounds adviseable if you think that there is a threading risk for this scenario (in most cases, there is no need to synchronize; but if you know this is a threaded area, then synchronizing is sensible).

    Also note that a common pattern when raising an event is to take a snapshot:

    protected virtual void OnSomeEvent() {
        EventHandler handler = SomeEvent; // assume the "field-like" version
        if(handler!=null) handler(this, EventArgs.Empty);
    }
    

    My point here is that it is in theory possible for a subscriber to receive an event even after they think they have unsubscribed, so if extreme cases you might want to handle this manually, perhaps by keeping a flag somewhere (or just exception handling).


    As requested, last code fragment in VB (via reflector) for comparison:

    Protected Overridable Sub OnSomeEvent()
        Dim handler As EventHandler = Me.SomeEvent
        If (Not handler Is Nothing) Then
            handler.Invoke(Me, EventArgs.Empty)
        End If
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 207k
  • Answers 207k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You seek the alternation operator, indicated with pipe character: |… May 12, 2026 at 9:24 pm
  • Editorial Team
    Editorial Team added an answer Declaring an array of strings is easy, the same as… May 12, 2026 at 9:24 pm
  • Editorial Team
    Editorial Team added an answer This is toatlly lame, code, but just a random thought… May 12, 2026 at 9:24 pm

Related Questions

When using a DataReader object to access data from a database (such as SQL
When using a browser to transform XML (Google Chrome or IE7) is it possible
When using a Zend_Form , the only way to validate that an input is
When using a file attachment control on a InfoPath form as part of a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.