Link to home
Start Free TrialLog in
Avatar of AlexFM
AlexFM

asked on

Asynchronous event handling

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?
Avatar of enwhysee
enwhysee

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 } );
      }
}
Avatar of AlexFM

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of devsolns
devsolns

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of AlexFM

ASKER

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?
Avatar of AlexFM

ASKER

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.