Home > Development > Handling Causality with APM and Not ThreadPool QueueUserWorkItem

Handling Causality with APM and Not ThreadPool QueueUserWorkItem


using System;
using System.Runtime.Remoting.Messaging;

namespace AsynchronousProgrammingModelSamples
{

    public class ApmAsychExecution
    {
        public void ExecuteAsynchronouslyWithCallback<TResult>(Func<TResult> function, Action<TResult> callback,
                                                              Action<Exception> exception)
        {
            TResult result = default(TResult);

            Action action = () =>
                                {
                                    result = function();
                                };

            action.BeginInvoke(fn =>
                                  {

                                      try
                                      {
                                          AsyncResult aresult = (AsyncResult) fn;
                                          Action caller = (Action) aresult.AsyncDelegate;
                                          caller.EndInvoke(aresult);
                                          callback(result);
                                      }catch(Exception e)
                                      {
                                          exception(e);
                                      }
                                  }, null);

        }

        public void ExecuteAsynchronouslyWithCallback<TArg, TResult>(Func<TArg,TResult> function, Action<TResult> callback,
                                                              Action<Exception> exception, TArg argument)
        {
            TResult result = default(TResult);

            Action action = () =>
            {
                result = function(argument);
            };

            action.BeginInvoke(fn =>
            {

                try
                {
                    AsyncResult aresult = (AsyncResult)fn;
                    Action caller = (Action)aresult.AsyncDelegate;
                    caller.EndInvoke(aresult);
                    callback(result);
                }
                catch (Exception e)
                {
                    exception(e);
                }
            }, null);

        }
    }

    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public override string ToString()
        {
            return String.Format("Id: {0}, Name: {1}", Id, Name);
        }
    }

    public class Program
    {
        public static void Main()
        {
            Program p = new Program();
            p.ExecuteItemAsyncronously();

            Console.WriteLine("Press any key to quit..");
            Console.ReadKey();
        }

        public void ExecuteItemAsyncronously()
        {
            ApmAsychExecution apm = new ApmAsychExecution();
            apm.ExecuteAsynchronouslyWithCallback(
                    () => new Customer { Id = 1, Name = "Blair Davidson" }, Console.WriteLine, Console.WriteLine
                );
            apm.ExecuteAsynchronouslyWithCallback(
                    name => new Customer { Id = 1, Name = name }, Console.WriteLine, Console.WriteLine, "Patrick Smith"
                );
            apm.ExecuteAsynchronouslyWithCallback(
                    () =>
                        {
                            throw new InvalidOperationException("Error Testing....");
                            return new Customer {Id = 1, Name = "Blair Davidson"};
                        }, Console.WriteLine, Console.WriteLine
                );
        }
    }
}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: