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

  • SEARCH
  • Home
  • 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 8526289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:13:48+00:00 2026-06-11T08:13:48+00:00

I keep seeing this pattern of invoking events in other people’s code: void OnMyEvent()

  • 0

I keep seeing this pattern of invoking events in other people’s code:

void OnMyEvent()
{
  var myEvent = MyEvent;

  if (myEvent != null)
    myEvent(this, EventArgs.Empty);
}

What is the benefit of using a variable in order to reference the event MyEvent, rather than directly using MyEvent(this, EventArgs.Empty)?

  • 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-06-11T08:13:50+00:00Added an answer on June 11, 2026 at 8:13 am

    In a multi threaded application, the event may get unsubscribed from in the middle of the call on this method.

    This can cause a NullReferenceException if the event handler is not copied in this manner.

    void OnMyEvent()
    {
      if (MyEvent!= null)                // Thread A checks event 
      {                                  // Thread B unsubscribes _last_ handler 
        MyEvent(this, EventArgs.Empty);  // Boom!
      }
    }
    

    But with:

    void OnMyEvent()
    {
      var myEvent = MyEvent;             // Thread A gets _copy_ of invocation list
      if (myEvent != null)               // Using copy, so no problem 
      {                                  // Thread B unsubscribes _last_ handler
        myEvent(this, EventArgs.Empty);  // Still using copy, so no problem 
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn about this feature of javascript I keep seeing in code,
Reading some piece of code and I keep seeing this : public override bool
In this Java project I'm looking at, I keep seeing code with HashMap, like
I keep seeing this kind of code a lot in python setup.py files. from
I keep seeing code that does checks like this if (IsGood == false) {
Possible Duplicate: new MyObject(); vs new MyObject; I keep seeing code like this one:
I'm trying to run a Java Jetty application and I keep seeing this error:
I am maintaining some legacy Visual Basic ASP code and I keep seeing <%
I have a about brackets like this {} in VB.net. I keep seeing syntax
I've got a pretty noob question for ya. I keep seeing this syntax: <%=

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.