.Net Framework, Asp.net, Ado.net, .Net Remoting, .Net Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML

Ado.netAsp.Net 2.0SQLC-SharpXMLFrameworkIISXMLWebServiceArchitecture
UMLProject ManagementSDLCMethodologiesDesign PatterenOOPWCF.Net RemotingWWF
MVPMVCSilver LightN-HibernateAjaxJ-QuerySEO-MarketingSite MapPhp

Asyc call in Asp.net 2.0

How to make Asyc method in Asp.net 2.0 ?

System.Threading || Multi threading || Asyc Call in Asp.net || Callback Example

Multithreading is a technique that can be used to perform time consuming tasks (like a I/O operation, sending bulk mails) in a separate additional thread other than the main application thread.


Asyc call in asp.net 2.0
public partial class Asyc_call_Aspnet : System.Web.UI.Page
1: {
2:
3: public delegate void AsyncTaskDelegate();
4: AsyncTaskDelegate _runnerDelegate = null;
5:
6:
7: // Asyc task can be registered with any even handler, either in page_load
8: protected void Page_Load(object sender, EventArgs e)
9: {
10: if (!IsPostBack)
11: {
12: // Page.RegisterAsyncTask(new PageAsyncTask(new BeginEventHandler(this.BeginRequest), new EndEventHandler(this.EndRequest), new EndEventHandler(this.TimeoutHandler), true));
13:
14: }
15: }
16:
17:
18: // OR with any button click
19: protected void btnStartAsyc_Click(object sender, EventArgs e)
20: {
21: Page.RegisterAsyncTask(new PageAsyncTask(new BeginEventHandler(this.BeginRequest), new EndEventHandler(this.EndRequest), new EndEventHandler(this.TimeoutHandler), true));
22: }
23:
24:
25: // This can be Email Sending Process with database interaction which may take long time.
26: void MyLongTask()
27: {
28:
29: for (Int64 i = 0; i < 100; i++)
30: {
31: string _str= i.ToString() + " Time " + DateTime.Now.Millisecond.ToString();
32:
33: }
34: }
35:
36:
37:
38: // Here the request begin
39: IAsyncResult BeginRequest(object sender, EventArgs e, AsyncCallback cb, object extraData)
40: {
41: _runnerDelegate = new AsyncTaskDelegate(this.MyLongTask);
42:
43: IAsyncResult result = _runnerDelegate.BeginInvoke(cb, extraData);
44:
45: return result;
46: }
47:
48: // Once task is over we can do whatever need.
49: void EndRequest(IAsyncResult result)
50: {
51: _runnerDelegate.EndInvoke(result);
52:
53: lblMessage.Text += "Asyc call is done.";
54:
55: }
56:
57:
58: void TimeoutHandler(IAsyncResult asyncResult)
59: {
60:
61: Response.Write("<strong>async Request timed out</strong><br />");
62:
63: }
64:}
65:
Leave your comment.
Name
Email
Comments
Tell Us6278+5 =

Reduce your Dialy IT Cost, Consult with us
Stay healthy in recession

Advertisement
Sponsored by

Privacy Policy ©2009 ETG Consultancy, All Rights Reserved Terms & Conditions
Asp.net, Ado.net, .Net Remoting, .Net Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML