Skip to content

Instantly share code, notes, and snippets.

@kevingosse
Created June 25, 2019 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevingosse/7a9325655e67f82851110d5283f6025c to your computer and use it in GitHub Desktop.
Save kevingosse/7a9325655e67f82851110d5283f6025c to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
private static ManualResetEventSlim _mutex = new ManualResetEventSlim();
private static Task _sharedTask;
static void Main(string[] args)
{
var tcs = new TaskCompletionSource<bool>();
_sharedTask = tcs.Task;
Task.Run(Thread1);
Thread.Sleep(2000); // Wait for the task to start
Task.Run(Thread2);
Thread.Sleep(2000); // Wait for the task to start
Console.WriteLine("Completing the shared task");
tcs.SetResult(true);
Console.WriteLine("Will never get there");
}
private static async Task Thread1()
{
await _sharedTask;
_mutex.Wait();
Console.WriteLine("Will never get there");
}
private static async Task Thread2()
{
// Task.WhenAll won't be completed because the completion thread is blocked in Thread1()
Task.WhenAll(_sharedTask).ContinueWith(_ =>
{
_mutex.Set();
Console.WriteLine("Will never get there");
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment