Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegating a task in and getting notified when it completes (in C#)

Conceptually, I would like to accomplish the following but have had trouble understand how to code it properly in C#:


SomeMethod { // Member of AClass{}
    DoSomething;
    Start WorkerMethod() from BClass in another thread;
    DoSomethingElse;
}

Then, when WorkerMethod() is complete, run this:


void SomeOtherMethod()  // Also member of AClass{}
{ ... }

Can anyone please give an example of that?

like image 799
greg7gkb Avatar asked Sep 16 '08 17:09

greg7gkb


People also ask

What will happen if a delegate has a non void return type?

When the return type is not void as above in my case it is int. Methods with Int return types are added to the delegate instance and will be executed as per the addition sequence but the variable that is holding the return type value will have the value return from the method that is executed at the end.

What is the difference between func string string and delegate?

Func is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter. The last parameter is considered as an out parameter. This delegate can point to a method that takes up to 16 Parameters and returns a value.

What is public delegate void?

public delegate void Del(string message); A delegate object is normally constructed by providing the name of the method the delegate will wrap, or with a lambda expression. Once a delegate is instantiated, a method call made to the delegate will be passed by the delegate to that method.

How do you declare a delegate?

A delegate can be declared using the delegate keyword followed by a function signature, as shown below. The following declares a delegate named MyDelegate . public delegate void MyDelegate(string msg); Above, we have declared a delegate MyDelegate with a void return type and a string parameter.


2 Answers

The BackgroundWorker class was added to .NET 2.0 for this exact purpose.

In a nutshell you do:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate { myBClass.DoHardWork(); }
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(SomeOtherMethod);
worker.RunWorkerAsync();

You can also add fancy stuff like cancellation and progress reporting if you want :)

like image 99
Isak Savo Avatar answered Oct 19 '22 12:10

Isak Savo


In .Net 2 the BackgroundWorker was introduced, this makes running async operations really easy:

BackgroundWorker bw = new BackgroundWorker { WorkerReportsProgress = true };

bw.DoWork += (sender, e) => 
   {
       //what happens here must not touch the form
       //as it's in a different thread
   };

bw.ProgressChanged += ( sender, e ) =>
   {
       //update progress bars here
   };

bw.RunWorkerCompleted += (sender, e) => 
   {
       //now you're back in the UI thread you can update the form
       //remember to dispose of bw now
   };

worker.RunWorkerAsync();

In .Net 1 you have to use threads.

like image 33
Keith Avatar answered Oct 19 '22 12:10

Keith