Manually control task completion with TaskCompletionSource 0 ▲ Siderite's Blog 2 hours ago · Tech · hide · 0 comments TaskCompletionSource and TaskCompletionSource<T> are types that allow you to create an unbound Task, which means it’s not tied to a delegate. This gives you manual control over when and how the task completes. You can manually set the task’s result, or signal that the task has completed due to cancellation or an error. What does it mean? Well, I had this flaky integration test - flaky is a fashionable term for a test that randomly fails - that checked a semaphore system worked (the process had to only be allowed to start if there wasn't one already in progress). It used a Delay task, which would normally work, until something on the test machine made the code run faster or slower, and then it failed. It ran the process in a thread, then started the process again in another thread and expected it to return false. So how did I solve it? I've set up a TaskCompletionSource, then I've mocked a service that was used after the semaphore was tested and created to SetResult for the underlying… No comments yet. Log in to reply on the Fediverse. Comments will appear here.