Intro The msdn API reference defines the Task class as a representation of an asynchronous operation. However, as soon as you start writing Task based code for asynchronous/parallel programming, or simply offloading work to a thread pool thread, it becomes obvious that Task has more than one face based on the use case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* 1- Offloading work to a ThreadPool thread */ Task<int> task = new Task<int>(() => SumMethod()); public int SumMethod() { int sum; //... return sum; } /* 2- Asynchronous */ int task = await SumMethodAsync(); public Task<int> SumMethodAsync() { int sum; //... return sum; } |
Task Faces Active…