There are two types of asynchronous operations in .NET.
- I/O bound
- CPU bound
For I/O bound async
/await
syntax should be used. TPL should not be used for this. async
methods does not spawns new thread to hand le I/O work asynchronously.
For CPU bound operations TPL should be used (Task.Run
). This spawns new thread. Performance testing should be done to check if overhead of new thread and context switching is not greater than actual work to be done.
- CPU bound should be done in thread pool (
Task.Run
orParallel.ForEach
) - I/O bound should not
- UI thread should do minimal work (mostly update UI or react to user actions)
- extensive computations should be done on different thread (CPU bound).
- I/O waits should be awaited (less overhead since
ThreadPool
have some overhead) - if I/O task are awaited then it is easier to update UI with new data (only one thread is allowed to update UI – main one)
Further read: