private Task<bool> DoWork( DoWorkArguments arguments) { Console.WriteLine("{0:mm:ss.ffffff} [{1}] Doing work for sequence {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, arguments.Sequence); var _result = new TaskCompletionSource<bool>(); new Task(() => { Console.WriteLine("{0:mm:ss.ffffff} [{1}] :: Starting for sequence {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, arguments.Sequence); Thread.Sleep(arguments.SleepInterval); if (arguments.ShouldThrowException) { Console.WriteLine("{0:mm:ss.ffffff} [{1}] :: Sequence {2} is exceptional !", DateTime.Now, Thread.CurrentThread.ManagedThreadId, arguments.Sequence); var _exception = new ApplicationException("Bang !"); _result.SetException(_exception); } else { _result.SetResult(arguments.ResultToReturn); } Console.WriteLine("{0:mm:ss.ffffff} [{1}] :: Completed for sequence {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, arguments.Sequence); }).Start(); Console.WriteLine("{0:mm:ss.ffffff} [{1}] Exiting for sequence {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, arguments.Sequence); return _result.Task; }
private Task <bool> DoWork( DoWorkArguments arguments) { Console.WriteLine("{0:mm:ss.ffffff} [{1}] Doing work for sequence {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, arguments.Sequence); var _result = new TaskCompletionSource <bool>(); new Task(() => { Console.WriteLine("{0:mm:ss.ffffff} [{1}] :: Starting for sequence {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, arguments.Sequence); Thread.Sleep(arguments.SleepInterval); if (arguments.ShouldThrowException) { Console.WriteLine("{0:mm:ss.ffffff} [{1}] :: Sequence {2} is exceptional !", DateTime.Now, Thread.CurrentThread.ManagedThreadId, arguments.Sequence); var _exception = new ApplicationException("Bang !"); _result.SetException(_exception); } else { _result.SetResult(arguments.ResultToReturn); } Console.WriteLine("{0:mm:ss.ffffff} [{1}] :: Completed for sequence {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, arguments.Sequence); }).Start(); Console.WriteLine("{0:mm:ss.ffffff} [{1}] Exiting for sequence {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, arguments.Sequence); return(_result.Task); }