static void Main(string[] args) { var myParam = new MyParam { Id = 1, }; var basicCommand = new BasicCommand2(); int i2 = 0; var actions = System.Linq.Enumerable.Range(0, 10).Select(i => new Action(() => basicCommand.SendCommand(new MyParam { Id = i, }))); foreach (Action a in actions) { Console.WriteLine("Starting {0}", i2++); a.Invoke(); } Console.ReadLine(); }
public void SendCommand(MyParam param) { ProcessingCommand += OnProcessingCommand; Func<RequestCommand, ResponseCompleteCommand> worker = (inFuncRequest) => { Console.WriteLine("In Worker Id:{0}", inFuncRequest.Id); Console.WriteLine("In Worker ... Calling Event Id:{0}", inFuncRequest.Id); OnProcessingCommand(this, new ResponseCompleteEvent { Id = inFuncRequest.Id, }); return new ResponseCompleteCommand { Id = inFuncRequest.Id, }; }; Action<IAsyncResult> actionOnComplete = (requestInOnComplete) => { Console.WriteLine("In actionOnComplete"); 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}", myParam.Id); var inlineWorker = asyncResult.AsyncDelegate as Func<RequestCommand, ResponseCompleteCommand>; Console.WriteLine("In actionOnComplete EndInvoke..."); ResponseCompleteCommand commandResponse = worker.EndInvoke(requestInOnComplete); Console.WriteLine("In actionOnComplete EndInvoke... Done! param.Id:{0} == responseComplete.Id:{1} ==> {2}", myParam.Id, commandResponse.Id, myParam.Id == commandResponse.Id); }; AsyncOperation asyncOperation = AsyncOperationManager.CreateOperation(new UserState { MyParam = param, }); var asyncCallback = new AsyncCallback(actionOnComplete); worker.BeginInvoke(new RequestCommand { Id = param.Id, }, asyncCallback, asyncOperation); }