We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Asynchronous event handling

AlexFM asked
Medium Priority
Last Modified: 2012-06-27
I have Windows Form class which creates another class (server) and subscribes to it's events. Some of these events are raised from worker threads. I don't like the fact that events are handled synchronously, this can cause deadlock. I would prefer to handle some of these events asynchronously, with minimal changes in existing code.
I think about two ways:
1) Instead of raising events from server, invoke form method asynchronously using BeginInvoke. But in this case server must have knowledge about client, and this is really bad.
2) Client can decide whether to handle event asynchronously. In some event handlers client invokes another method using BeginInvoke, and immidiately returns. Something like this:

void OnAsynchronousEvent(int n1, int n2)
     this.BeginInvoke(new MyDelegate(this.ActualEventHandler), new object[]{n1, n2});

void ActualEventHandler(int n1, int n2)
    // do something here while worker thread continues

The second way looks good for me, but I am afraid that I am missing some language feature which allows to do this better. Is there better way?
Watch Question

You've got the general idea down, although is OnAsynchronousEvent a handler for something else? Here's a short code sample to show the simplest usage of this concept:

public delegate void DelegateSomeEvent(int n1, int n2);

public class Foo : System.Windows.Forms.Form
      public void OnSomeEvent(int n1, int n2)
          // update some UI control etc.

      private void btnDoStuff_Click(object sender, System.EventArgs e)
             Thread foo = new Thread(new ThreadStart(ThreadFunction));

      private void ThreadFunction()
             BeginInvoke(new DelegateSomeEvent(this.OnSomeEvent), new object[] { 1, 2 } );


This is exactly what I don't want to do. Server (ThreadFunction) must know name of client's handler function (OnSomeEvent). In my case server and client are different classes, and server doesn't know anything about client. Events allow to do this.
Another possible way to improve performance is to invoke the event delegate asynchronously from within the class defining the event.

protected void TriggerEvent()
      (OnEvent != null)
            IAsyncResult ar = OnEvent.BeginInvoke(null, null, null, null);
            // usual way - > OnEvent(null, null);
                                //do some work here

public event EventHandler OnEvent = null;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

If your concerned with having to wait and call EndInvoke (which you should definatly do) you can implement a fire and forget wrapper that will handle cleanup (calling endinvoke) automatically for you.


This looks like what I need, but I cannot compile this. Event is raised from C++/CLI code:

OnParametersChangedAS(n1, n2);

When I replace it with:

OnParametersChangedAS->BeginInvoke(exposure, gain, nullptr, nullptr);

I get C3918 error. Can you give me working example in C#, I can translate it to C++. In your sample, what is the class to which BeginInvoke belongs?


Actually, your code is working. It is my problem now to translate it to C++.

cool, let me know if you need anything further.  take care.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.