Skip to content

Instantly share code, notes, and snippets.

@halter73
Created May 11, 2018 03:04
Show Gist options
  • Save halter73/6270b0c90372df2e1d2302b026b6cb39 to your computer and use it in GitHub Desktop.
Save halter73/6270b0c90372df2e1d2302b026b6cb39 to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace RunAsync
{
class Program
{
static void Main(string[] args)
{
for (var i = 0; i < 1000; i++)
{
TestRunAsync(i).Wait();
}
Console.WriteLine("Done!");
}
static async Task TestRunAsync(int testRun)
{
var tcs = new TaskCompletionSource<object>(TaskContinuationOptions.RunContinuationsAsynchronously);
var mre = new ManualResetEventSlim();
var tcsAwaiterTask = Task.Run(async () =>
{
await tcs.Task;
if (!mre.Wait(TimeSpan.FromSeconds(10)))
{
Console.WriteLine("Tcs continuation ran inline in test run {0}. Stack trace: {1}", testRun, Environment.StackTrace);
throw new Exception();
}
});
var tcsSetterTask = Task.Run(() =>
{
tcs.SetResult(null);
mre.Set();
});
await tcsAwaiterTask;
await tcsSetterTask;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment