?
Solved

Asynchronous event handling

Posted on 2006-04-05
7
Medium Priority
?
858 Views
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?
0
Comment
Question by:AlexFM
  • 3
  • 3
7 Comments
 
LVL 6

Expert Comment

by:enwhysee
ID: 16381079
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));
             foo.Start();
      }

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

Author Comment

by:AlexFM
ID: 16381131
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.
0
 
LVL 13

Accepted Solution

by:
devsolns earned 2000 total points
ID: 16381158
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
                                OnEvent.EndInvoke(ar)
      }
}                  

public event EventHandler OnEvent = null;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:devsolns
ID: 16381255
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.
0
 
LVL 48

Author Comment

by:AlexFM
ID: 16381358
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?
0
 
LVL 48

Author Comment

by:AlexFM
ID: 16381464
Actually, your code is working. It is my problem now to translate it to C++.
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16381493
cool, let me know if you need anything further.  take care.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question