AsyncResult class with a constructor in C#

wadaHeannafek

New Member
I am looking to instantiating an AsyncResult object but there seems to be no AsyncResult class with a constructor in .Net Frameowork that I can instantiate.Can someone provide a ready implementation of IAsynResult interface in C#? Or is there some trick way to instantiate an AsyncResult object?Edit 1: I am creating an async task within an ASP.Net page. In the begin method, I call a data layer static method called 'AsyncBeginGetSummary'. I would normally use ADO.Net BeginExecuteReader within this static method, which would work perfectly and I wouldn't need to instantiate an AsyncResult object. But, if the data object exists in Cache in ASP.Net app, then from this static method of ''AsyncBeginGetSummary', I do not want to call BeginExecuteReader. But then, I still need to return AsyncResult object so the End method of the task in ASP.Net can be called. My ASP.Net page code is like below.\[code\] IAsyncResult BeginAsyncOperation1(object sender, EventArgs e, AsyncCallback cb, object state) { //call a static method from DAL that returns IAsyncResult. //call 'AsyncBeginGetSummary' static method in DAL } void EndAsyncOperation1(IAsyncResult ar) { //call a static method in DAL that gets your data here //call 'AsyncEndGetSummary' static method in DAL }\[/code\]Edit 2: I have the following where I do not need to bother about instantiating AsyncResult object since I use a dummy method delegate to call BeginInvoke.\[code\] IAsyncResult BeginAsyncOperation1(object sender, EventArgs e, AsyncCallback cb, object state) { //call a static method from DAL that return IAsyncResult. if((Cache["abc"] !=null) { return (new dummy(DummyMethod)).BeginInvoke(cb, new FinalDataForDocumentsSummary(listFromCache.Count, listFromCache)); } return NFM.Data.Mappers.DocumentsSummaryByProgramDataMapper.AsyncBeginEndDocumentsSummaryByProgramAll( vendorId, cb); } void EndAsyncOperation1(IAsyncResult ar) { //call a static method in DAL that gets your data here } public delegate void dummy(); private void DummyMethod() { }\[/code\]Edit 3: I have used the following AsyncResult implementation in WCF. I guess I could use it in ASP.Net also.\[code\] class AsyncResult<T> : IAsyncResult{ private T data; private object state; private bool isCompleted = false; private AutoResetEvent waitHandle; private bool isSynchronous = false; public T Data { set { data = http://stackoverflow.com/questions/14087682/value; } get { return data; } } public AsyncResult(bool synchronous, object stateData) { isSynchronous = synchronous; state = stateData; } public void Complete() { isCompleted = true; ((AutoResetEvent)AsyncWaitHandle).Set(); } public object AsyncState { get { return state; } } public WaitHandle AsyncWaitHandle { get { if (waitHandle == null) waitHandle = new AutoResetEvent(false); return waitHandle; } } public bool CompletedSynchronously { get { if (!isCompleted) return false; else return isSynchronous; } } public bool IsCompleted { get { return isCompleted; } }}\[/code\]
 
Back
Top