title: AsyncResult class with a constructor in C#
body: <p>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.</p>
  
  <p>Can someone provide a ready implementation of IAsynResult interface in C#? Or is there some trick way to instantiate an AsyncResult object?</p>
  
  <p><strong>Edit 1:</strong> 
  I am creating an async task within an ASP.Net page. In the begin method, I call a data layer static method called 'AsyncBeginGetSummary'. </p>
  
  <p><strong>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.</strong> 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. </p>
  
  <p>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.</p>
  
  <pre><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></pre>
  
  <p><strong>Edit 2:</strong> 
  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.</p>
  
  <pre><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></pre>
  
  <p><strong>Edit 3:</strong> I have used the following AsyncResult implementation in WCF. I guess I could use it in ASP.Net also.</p>
  
  <pre><code> class AsyncResult&lt;T&gt; : 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; }
      }
  }
  </code></pre>