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 6238103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:09:03+00:00 2026-05-24T11:09:03+00:00

I have the following EventHandler: private EventHandler<MyEventArgs> _myEventHandler; public event EventHandler<MyEventArgs> MyEvent { add

  • 0

I have the following EventHandler:

private EventHandler<MyEventArgs> _myEventHandler;
public event EventHandler<MyEventArgs> MyEvent
{
  add { _myEventHandler += value; }
  remove { _myEventHandler -= value; }
}  

Could somebody explain the difference between the following snippets?
Snippet EventHandler (A):

//Snippet A:
if (_myEventHandler != null)
{
  _myEventHandler(new MyEventArgs());
}

Snippet BeginInvoke (B):

//Snippet B:
if (_myEventHandler != null)
{
  _myEventHandler.BeginInvoke(new MyEventArgs(), ar =>
  {
    var del = (EventHandler<MyEventArgs>)ar.AsyncState;
    del.EndInvoke(ar);
  }, _myEventHandler);
}

For clarification: What’s the difference between invoking an EventHandler “just as it is” and using BeginInvoke?

  • 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-24T11:09:04+00:00Added an answer on May 24, 2026 at 11:09 am

    The BeginInvoke approach is async, meaning that it is raised on a different thread. This can be dangerous if people don’t expect it, and is pretty rare for events – but it can be useful.

    Also, note that strictly speaking you should snapshot the event handler value – this is especially true if (via Begin*) you are dealing with threads.

    var tmp = _myEventHandler;
    if(tmp != null) {
        tmp(sender, args);
    }
    

    Also – note that your event subscription itself is not thread-safe; again, this only matters if you are dealing with multi-threading, but the inbuilt field-like event is thread-safe:

    public event EventHandler<MyEventArgs> MyEvent; // <===== done; nothing more
    

    The issues avoided here are:

    • with the snapshot, we avoid the risk of the last subscriber unsubscribing between the null-check and the invoke (it does mean they might get an event they didn’t expect, but it means we don’t kill the raising thread)
    • with the field-like event change we avoid the risk of losing subscriptions / unsubscriptions when two threads are doing this at the same time
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On the MSDN, I have found following: public event EventHandler<MyEventArgs> SampleEvent; public void DemoEvent(string
Assume that I have a class that exposes the following event: public event EventHandler
I am using C# 3.0. Following the standard event pattern I have: public event
I have the following: <select id=price_select></select> I have an event handler that invokes the
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code where I am handling an event twice. However I
I have the following Silverlight code: [ScriptableType] public partial class Page : UserControl {
I have the following code in one of my Modules : public class MyModule
Possible Duplicate: .Net Changes the element IDs I have the following problem (I'll explain
I have tried to restrict Following EventHandler In specific Columns of Datagridview but fail

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.