Pages

Search

Friday, December 19, 2008

How to make asynchronous calls in .NET ?

Method calls what we build in applications are always synchronous. I mean
Ex:-

Main()
{
Fun2();
Fun3();
.
.
.
}

Fun2()
{
.
.
}
Fun3()
{
.
.
}
In this in Main function, first function Fun2() is called but Fun3() is called only Fun2() is completed and returned.
Though Fun3() does not required input from Fun2() but it has to wait.

When we build windows application, there might me a logic when user clicks on a button a process runs to perform operation.
Consider if it is a long running process, while process is running and if user disturbs the window or User interface, the screen gets hanged saying “not responding”.

When user moves the screen while a process is being run synchronously by application, then
Since Moving window is also a process that has to run but it will wait till the long running process is completed.
In above example,
Fun2() is long running process and Fun3() is the function that has to be runned when user moves the window or UI.
So if I make Fun2() to run asynchronously then Fun3() can be executed when and then required.

So in windows application long running process need to run in a thread or asynchronously, where UI is still interactive.

Delegates help to make a call asynchronously.
Since delegates store the address of the method, we need to invoke a delegate that handles the memory address of the method stored by it to processor which runs it concurrently or separately.

So in windows application when a separate thread or long running process is called asynchronously or runned separately, this process cannot assign values to window components which are on different thread.











Ex:-
//delegate to store the long running method address
private delegate void dlgDelayer();

//button click from user interface
private void btnStart_Click(object sender, EventArgs e)
{
//creating an nstance for delegate with address of long running //method or process
dlgDelayer objDel = new dlgDelayer(this.Delay);
//Invoking delegate
//This mean to initiate the process or method pointed by delegate
//AsyncCallback mean to call methos “ProcessDone” when long running //method is completed
objDel.BeginInvoke(new AsyncCallback(ProcessDone), objDel);
}
//Long running method
private void Delay()
{
int i = 1;
while (true)
{
string tt = lblProcess.Text;
i++;
UpdateLabel( i.ToString());
Thread.Sleep(2000);
if (i == 8)
break;
}
//To update a label (windows component
UpdateLabel( "Done");
}
//Method that gets called when long running method is completed
private void ProcessDone(IAsyncResult ar)
{
((dlgDelayer)ar.AsyncState).EndInvoke(ar);
//To update a label (windows component
UpdateLabel("Sucess");
}
//delegate to update windows label
delegate void StringParameterDelegate(string value);

private void UpdateLabel(string strMsg)
{
//varaible to check whether the caller has to invoke, when caller is //running on separate thead to where control is created
if (InvokeRequired)
{
//When caller method has to invoke or call the function //asynchronously
BeginInvoke(new StringParameterDelegate(UpdateLabel), new object[] { strMsg});
return;
}
//Updating label when caller running in same thread where the label
//control is
lblProcess.Text = strMsg;
}

No comments:

Post a Comment