title: AsyncCallback, call from different class and multiple forms
body: <p>I'm trying to build simple class using <a href="http://codebender.denniland.com/a-singleton-base-class-to-implement-the-singleton-pattern-in-c/" rel="nofollow">Singleton Pattern</a>.</p>
  
  <p>What I'm trying to accomplish is to have in my main class 2 functions responsible for handling results of async method call. One will handle correct results and second all errors.</p>
  
  <p>Class that does all async operations looks like this:</p>
  
  <pre><code>class EWS : SingletonBase&lt;EWS&gt;
  {
      private EWS()
      {
          //private constructor
      }
  
  
      private int LongRunningMethod(Action&lt;string&gt; error)
      {
          int x = 0;
          for (int i = 0; i &lt; 10; i++)
          {
              //Console.WriteLine(i);
              x += i;
              Thread.Sleep(1000);
          }
          //here I can do try...catch and then call error("Description")
          return x;
      }
  
      public class CommandAndCallback&lt;TSuccess, TError&gt;
      {
          public TSuccess Success { get; set; }
          public TError Error { get; set; }
      }
  
      public void DoOperation(Action&lt;int&gt; success, Action&lt;string&gt; error)
      {
          Func&lt;Action&lt;string&gt;, int&gt; dlgt = LongRunningMethod;
          //Func&lt;Action&lt;string&gt;, int&gt; dlgt = new Func&lt;Action&lt;string&gt;, int&gt;(LongRunningMethod);
  
          CommandAndCallback&lt;Action&lt;int&gt;, Action&lt;string&gt;&gt; callbacks = new CommandAndCallback&lt;Action&lt;int&gt;, Action&lt;string&gt;&gt;() { Success = success, Error = error };
  
          IAsyncResult ar = dlgt.BeginInvoke(error,MyAsyncCallback, callbacks); 
      }
  
      public void MyAsyncCallback(IAsyncResult ar)
      {
          //how to access success and error here???
          int s ;
          Func&lt;Action&lt;string&gt;, int&gt; dlgt = (Func&lt;Action&lt;string&gt;,int&gt;)ar.AsyncState;
          s = dlgt.EndInvoke(ar);
          //here I need to call success or error that were passed to DoOperation
      }
  }
  </code></pre>
  
  <p>In my main class I would like to call my method like so:</p>
  
  <pre><code>private void Operation_OK(int count)
  {
       //here handle OK
  }
  
  private void Operation_ERROR(string error)
  {
       //here handle ERROR
  }
  
  private void button1_Click(object sender, EventArgs e)
  {
      EWS.Instance.DoOperation(Operation_OK, Operation_ERROR);
  }
  </code></pre>
  
  <p><strong>How should I modify my EWS class so I can call it like shown above.<br>
  How to use lambda expressions instead delegates?<br></strong>
  Is this a good idea to call methods like this? I need to have 2 or 3 methods in my class and be able to call them in all forms independent. </p>