Question

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.

 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
    }

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.

 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()
    {
    }

Edit 3: I have used the following AsyncResult implementation in WCF. I guess I could use it in ASP.Net also.

 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 = 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; }
    }
}
Was it helpful?

Solution

It sounds like you want an IAsyncResult that indicates synchronous completion – that the result is already available.

In that case, you can create your own simple class that returns true for IsCompleted and CompletedSynchronously, and returns the caller's parameter in AsyncState, and null for WaitHandle.

If you want the caller to be able to use the WaitHandle, you'll need to create a ManualResetEvent and dispose it at some point; see this article.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top