Compartilhar via


Using Delegates in C#

I used to be an instructor some time back and still get a kick out helping to teach people and would like to explain one of the interesting new odds of C#. In C#, you do not have pointer any more. How do you deal with pointer to functions? This article explains this ….

 This is an old concept with a new name in C#/.NET world. So when do we use delegates in C#? What is a delegate? For a C/C++ professional this is nothing more than a pointer to function. No, this is not a class, but you need to use new to create it. When is pointer to function used in C, C++? Mostly is asynchronous programming, when the programmer expects a callback from a function, as an example, in thread, when an overlapped IO completes the writing etc.
The delegate is used in the same area in .NET/C# world. At the time when an event occurs or when something runs in a different thread. This article explains quickly how delegate is used in UI/ worker threads in C#/.NET.

Say, we are reading a very ArrayList and writing the data in an excel file – at the same time we want to update the progress bar in a dialog. If both of the operations are in a single thread, the dialog box will freeze although the status bar will keep going. Using delegate solves the freezing issue.
In order to define a delegate you need to do the following:

//delegate to run on a different worker thread
delegate void OpenExcelAndShowStausDelegate(ArrayList myData);

This defines a delegate, i.e., a pointer to function called OpenExcelAndShowStausDelegate that takes one ArrayList parameter.
From one of the functions in the program, the delegate should be called as follows:

:
:
//the creation and update to the excel is done via delegate
OpenExcelAndShowStausDelegate myDelegate = new OpenExcelAndShowStausDelegate(OpenExcelAndShowStatus);
myDelegate.BeginInvoke((XpeComponent)selectedComponent, null, null);
:
:

This starts another thread that calls the function OpenExcelAndShowStatus() that should open an excel spreadsheet and start writing the data.
OpenExcelAndShowStaus would be defined as follows:

Void OpenExcelAndShowStaus (ArrayList myData)
{
:
:
:
//Update the Progress bar
UpdateProgressBar(myArrayList.count, curRecNumber);
:
:
}

If the progress bar is updated from the same thread as reading, the problem remains the same. Hence, the progress bar should be updated also from another delegate.
In order to do this we need to create another delegate for the progress bar:

delegate void UpdateDelegate(int a_max, int a_cur);

This defines a pointer to a function or delegate that takes two integers, one for the max status value, second, the current status.

void UpdateProgressBar(int a_maximum, int a_cur)
{
if (pbStatus.InvokeRequired == false)
{
pbStatus.Maximum = a_maximum;
pbStatus.Value = a_cur;
}
else
{
//show progress asynchronously
UpdateDelegate myUpdateDelegate = new UpdateDelegate (UpdateProgressBar);
this.BeginInvoke(myUpdateDelegate, new object[]{a_maximum,a_cur});
}
}

 

Note: pbStatus is the name of the progress bar

Diagrammatically this can be shown as:

Rajib1_small

Hope this is useful to you!

- Rajib

Comments