Thursday, December 3, 2009

events and delegates

some useful thing on events and delegates...

//Declare a delegate (class member)
private delegate void UpdateProgressBar();

//Create an instance of the delegate for us to use (class member)
private UpdateProgressBar updateProgressDelegate;

void StepProgressBar()
{
progressBar.PerformStep();
}

//Method executed on a different thread
DoDatabaseUpdate()
{

//Associate a method to our delegate (defined above)
updateProgressDelegate = new UpdateProgressBar(StepProgressBar);
for (;;) //Some loop
{
//Code for the lengthy operation
...
...
this.Invoke(updateProgressDelegate );//Update the progress bar by calling the delegate
}
}