Async-await and WaitHandles
The TPL is great but sometimes you have to work within a framework which schedules background tasks for you without giving you access to a Task of some kind. I had a situation like the following 1 2 3 4 5 6 7 8 private void DoSomeWork(CancellationToken token) { token.ThrowIfCancellationRequested(); // do lots of work } // some other code that might happen in a UI thread AsyncWorkScheduler.Schedule(()=> DoSomeWork(token)); I didn’t really have access to how the background task was scheduled, and to be honest I didn’t really care. What I needed was a way to wait for the work to finish, whether it finished by itself or if it was cancelled via the CancellationToken. I wanted to use await to avoid blocking any UI threads as well. ...