public void ExecuteAsync(MyParam param) { MyCommandProcessor myCommandProcessor = new MyCommandProcessor(); myCommandProcessor.CommandStarted += OnCommandStarted; myCommandProcessor.CommandProcessed += OnCommandProcessed; myCommandProcessor.CommandEnded += OnCommandEnded; Action<Request> worker = r => myCommandProcessor.Process(r); Action<IAsyncResult> actionOnComplete = (requestInOnComplete) => { // Console.WriteLine("In actionOnComplete T:{0} - - IsThreadPool {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); var asyncResult = (AsyncResult)requestInOnComplete; var asyncOperationInOnComplete = requestInOnComplete.AsyncState as AsyncOperation; var userStateInOnComplete = asyncOperationInOnComplete.UserSuppliedState as UserState; var myParam = userStateInOnComplete.MyParam; //Console.WriteLine("In actionOnComplete Id:{0} T:{1}- IsThreadPool {2}", myParam.Id, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); var inlineWorker = asyncResult.AsyncDelegate as Action<Request>; //Console.WriteLine("In actionOnComplete EndInvoke... T:{0} - IsThreadPool {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); worker.EndInvoke(requestInOnComplete); // Console.WriteLine("In actionOnComplete EndInvoke... Done! param.Id:{0} == responseComplete.Id:{1} ==> {2} T:{3}", // myParam.Id, // commandResponse.Id, // myParam.Id == commandResponse.Id, // Thread.CurrentThread.ManagedThreadId); }; AsyncOperation asyncOperation = AsyncOperationManager.CreateOperation(new UserState { MyParam = param, }); var asyncCallback = new AsyncCallback(actionOnComplete); worker.BeginInvoke(new Request { Id = param.Id, MyParam = param, }, asyncCallback, asyncOperation); }
static void Main(string[] args) { var myParam1 = new MyParam { Id = 1, Process = "DownloadBigFile", Delay = 3000, }; var myParam2 = new MyParam { Id = 2, Process = "LoadingBigPicture", Delay = 500, }; var myParam3 = new MyParam { Id = 3, Process = "LoadingBigPicture", Delay = 1500, }; var myRouter = new MyRouter(); var myAsyncWorker = new MyAsyncWorker(myRouter); myAsyncWorker.ExecuteAsync(myParam1); myAsyncWorker.ExecuteAsync(myParam2); myAsyncWorker.ExecuteAsync(myParam3); Console.ReadLine(); }
public void RouteOnProcess(MyParam myParam) { if ("DownloadBigFile".Equals(myParam.Process)) { Console.WriteLine("{0} Downloading... {1}", DateTime.Now.ToString("mm:ss.ffff"), myParam); } if ("LoadingBigPicture".Equals(myParam.Process)) { Console.WriteLine("{0} Loading... {1}", DateTime.Now.ToString("mm:ss.ffff"), myParam); } }