.

asynchronous callback threading in c#

The long way:

static void Main()
{
  Func<string, int> method = TheWorkMethod;
  method.BeginInvoke ("test", Completed, method);
}

static int TheWorkMethod (string s) { return s.Length; }

static void Completed (IAsyncResult res)
{
  var target = (Func<string, int>) res.AsyncState;
  int result = target.EndInvoke (res);
  Console.WriteLine ("String length is: " + result);
}

For a function such as int DoFunction(string S):

Func<string, int> AsyncFunction = (input) =>
        {
          MessageBox.Show("Start " + input); 
          Thread.Sleep(5000); 
          return 5;
        };
AsyncFunction.BeginInvoke("InputText", funcResult =>
        {
            MessageBox.Show("Completed: result="+funcResult);
        }, 
        AsyncFunction);

For a method such as void DoMethod(string S):

Action<string> AsyncMethod = (input) => 
        { 
            MessageBox.Show("Start " + input); 
            Thread.Sleep(5000); 
        };
AsyncMethod.BeginInvoke("InputText", (asyncResult) => 
        { 
            MessageBox.Show("Completed: "+asyncResult.IsCompleted);
        }, 
        AsyncMethod);

What's your thoughts on this?

*

Protected by WP Anti Spam