Async Callback Delegate and Update UI from Secondary thread

Posted: April 16, 2005 in Uncategorized

Often you need requirement to implement callback with delagate and also to call some long runnign operation different from the main UI.THe requirement ofetn come to call a long database operation and still you want that to async way so that ur UI ll not hang.

Here is what the sample app is look like:-

here is a class named secondary and delegate declaration:go trhu the comment that is well enough.

 

namespace

threadtest

{

//delegate declaration for runnign the long task

delegate void testasync (int i);

//delaget declaration for processign task of main thread from sceondary thread

delegate void sendmessage(string msg);

public class secondary

{

//the long runnign method

public void methodonsecondary(int i)

{

for (int j=0;j<i;j++)

{

j=j+i;

}

i=i+1;

}

}

}

 

 

Now here goes he form code:-

testasync asy =

null;

private void button1_Click(object sender, System.EventArgs e)

{

//create an instance of the class whose method is a longrunnig app

secondary sec=

new secondary();

//create an instance of the delegate and pass the long runnign method

asy=

new testasync(sec.methodonsecondary);

//declare async callback and pass the callback method which will be raised when the long

//runnign app in secondary thread ll be completed

AsyncCallback callback=

new AsyncCallback(callbackmethod);

//invoke the long runnign app passing callback and value for the param

asy.BeginInvoke(500,callback,

null);

//YOU CAN WRITE ANY OTHER METHOD OF EXECUTIO HERE ITWILL BE EXECUTED AS PREV CALL IS ASYNC

}

private void callbackmethod(IAsyncResult ar)

{

try

{

//this method called once the secondary THREAD Complete

//call endinvoke pass the async result

//you can recd the ret value in an object array here if you want to manip the ret value of callback

asy.EndInvoke(ar);

//here goes our logic for updating main thread from secondary

//i am simply displayign a message here you can set some value that could be displayed in ur label

//or progress abar etc

UpdateUI("completee");

MessageBox.Show ("comp");

}

catch(Exception ex)

{

string msg="";

msg="error :"+ ex.Message.ToString();

UpdateUI(msg);

}

}

private void UpdateUI (string StatusMessage)

{

//if invoke is reqd on the thread

if (this.InvokeRequired )

{

//create delegate and pass the showmessage method

sendmessage handler=

new sendmessage(this.ShowMessage);

//arguments array to be passed to invoke method

Object[] args= {StatusMessage};

//invoke the method

this.BeginInvoke(handler,args);

}

else

{

ShowMessage(StatusMessage);

}

}

private void ShowMessage(string StatusMessage)

{

//show the result back to main thread

MessageBox.Show(StatusMessage);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s