public void MultiThreaded_TrySetComplete(bool isSetCompleteTest, int howManyThreads) { int continuationCalledCounter = 0; int completedGainedCounter = 0; int completedLossedCounter = 0; var taskSlim = new TaskSlim(null); taskSlim.SetContinuation(() => { Interlocked.Increment(ref continuationCalledCounter); }); var @syncEvent = new ManualResetEventSlim(false); for (int i = 0; i < howManyThreads; i++) { ThreadFactory.BackgroundThread((index) => { @syncEvent.Wait(); var result = isSetCompleteTest ? taskSlim.TrySetCompleted() : taskSlim.TrySetException(new Exception("bah")); if (result) { Interlocked.Increment(ref completedGainedCounter); } else { Interlocked.Increment(ref completedLossedCounter); } }, "T_" + i, i); } Thread.Sleep(0); @syncEvent.Set(); Thread.Sleep(1000); continuationCalledCounter.Should().Be(1, "continuation cannot be called more than once"); completedGainedCounter.Should().Be(1, "only 1 thread could have taken the lock successfully"); completedLossedCounter.Should().Be(howManyThreads - 1, "all other threads should have been unsuccessful"); }