Created
June 25, 2019 13:42
-
-
Save kevingosse/7a9325655e67f82851110d5283f6025c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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