Skip to main content

Posts

Showing posts with the label IAsyncResult

Asynchronous Execution in ASP.NET

Asynchronous Execution: Two ways either implement IHTTPAsyncHandler interface or in ASP.NET 2.0 set <%@ Page Async=”true” %>. The second option implements IHTTPAsyncHandler interface automatically for the page when parsed or compiled. AddOnPreRenderCompleteAsync ( new BeginEventHandler(BeginTask), new EndEventHandler(EndTask)); AddOnPreRenderCompleteAsync() shoud be called in Page_load. The BeginEventHandler and EndEventHandler are delegates defined as follows: IAsyncResult BeginEventHandler( object sender, EventArgs e, AsyncCallback cb, object state) void EndEventHandler( IAsyncResult ar) AsyncProcess starts and completes between PreRender and PreRenderComplete. Other way to perform Async Task is using AsyncPageTask structure. It also allows multiple tasks to execute simultaneously. void Page_Load (object sender, EventArgs e) { PageAsyncTask task = new PageAsyncTask( new BeginEventHandler(BeginTask), new EndEventH...

Asynchronous Programming Model(APM) with .Net

Namespace: System.Threading Asynchronous programming is simply allowing some portion of code to be executed on separate threads. Many .Net Framework classes support APM with methods like BeginXXX and EndXXX . FileStream FS = new Filestream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, FileOptions.Asynchronous); IAsyncResult vResult = FS.BeginRead(buffer, 0 , Buffer.length, null, null); //Do your work int NoOfBytes = FS.EndRead(vResult); FS.Close(); This type of APM is 1. Rendezvous Wait-Until-Done model . 2. Rendezvous Polling Model : Keeping doing some other work along with checking whether the APM work is completed or not. IAsyncResult vResult = FS.BeginRead(buffer, 0 , Buffer.length, null, null); While ( ! vResult.IsCompleted ) { //Do your work } int NoOfBytes = FS.EndRead(vResult); FS.Close(); 3. Callback Model :               ...