1

Delegates and events in the background work class

 2 years ago
source link: https://www.codesd.com/item/delegates-and-events-in-the-background-work-class.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Delegates and events in the background work class

advertisements
Thread1.WorkerReportsProgress = true;
Thread1.ProgressChanged += new ProgressChangedEventHandler(Function2HandleWhenProgressChanges); //When progress changes, define a function to handle  it.
Thread1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Function2HandleWhenThreadIsFinished); //The function to run when the thread is finished
Thread1.DoWork += new DoWorkEventHandler(Thread1_DoWorkDo); //The function defining what the thread must do.

Now, I understand that ProgressChangedEventHandler is a delegate. A delegate, in turn, turns out to be a class.

1. "ProgressChangedEventHandler" belongs to which class ? I have not defined any in my code.

2. Is "ProgressChanged" an event ? If so, which class does this belong to ?

3. If I don't specify "new ProgressChangedEventHandler" still the code compiles ?. Something like below.

 Thread1.WorkerReportsProgress = true;
 Thread1.ProgressChanged += Function2HandleWhenProgressChanges; //When progress changes,  define a function to handle  it.
 Thread1.RunWorkerCompleted += Function2HandleWhenThreadIsFinished; //The function to run when the thread is finished
 Thread1.DoWork += Thread1_DoWorkDo; //The function defining what the thread must do.


A delegate, in turn, turns out to be a class.

It is not a class, it is a type. Which explains why you ask these other questions. Think of it as a type description of a method, the return type and arguments are important. A delegate type helps the compiler to make sure you assign the right kind of event handler method. The method must have the exact same return type and arguments as the delegate type. The compiler complains if that's not the case. This kind of type safety is very important in .NET.

"ProgressChangedEventHandler" belongs to which class?

It is a delegate type, not a class. It is declared in the .NET framework. Adding a reference to System.dll and putting using System.ComponentModel at the top of your program allows you to use it without spelling out the full type name.

Is "ProgressChanged" an event ?

Yes, it is an event of the BackgroundWorker class. Along with DoWork and RunWorkerCompleted, two other events you almost always subscribe.

If I don't specify "new ProgressChangedEventHandler" still the code compiles ?.

That's called "syntax sugar". The C# compiler can tell what kind of delegate type is required from the event type and will automatically generate the "new ProgressChangedEventHandler" part of the statement as required. Very convenient. IntelliSense however will always generate it. Even the full statement is syntax sugar, you never explicitly assign the Delegate.Target property. It is inferred by the compiler to either null or this, depending on whether the target method is static or not.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK