Skip to content

Instantly share code, notes, and snippets.

@robdmoore
Last active December 21, 2015 03:18
Show Gist options
  • Save robdmoore/6240712 to your computer and use it in GitHub Desktop.
Save robdmoore/6240712 to your computer and use it in GitHub Desktop.
Playing with sync context and async
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace SyncContext
{
class Program
{
static void Main()
{
Run();
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
Run();
Console.Read();
}
private static void Run()
{
Console.WriteLine("=== Start");
OutputSyncContext();
Console.WriteLine("== Wait");
Start().Wait();
Console.WriteLine("== Wait long-running task");
Task.Factory.StartNew(async () => await Start(), TaskCreationOptions.LongRunning).Wait();
}
private static async Task Start()
{
OutputSyncContext();
var tasks = Enumerable.Range(1, 10).Select(i => Run(i)).ToArray();
await Task.WhenAll(tasks);
}
private static async Task Run(int i)
{
Console.WriteLine("{0}: Before continuation {1}", Thread.CurrentThread.ManagedThreadId, i);
await Task.Delay(200);
Console.WriteLine("{0}: After continuation {1}", Thread.CurrentThread.ManagedThreadId, i);
}
private static void OutputSyncContext()
{
if (SynchronizationContext.Current == null)
Console.WriteLine("No Sync context");
else
Console.WriteLine("Sync context hash code: " + SynchronizationContext.Current.GetHashCode());
}
}
}
@robdmoore
Copy link
Author

=== Start
No Sync context
== Wait
No Sync context
9: Before continuation 1
9: Before continuation 2
9: Before continuation 3
9: Before continuation 4
9: Before continuation 5
9: Before continuation 6
9: Before continuation 7
9: Before continuation 8
9: Before continuation 9
9: Before continuation 10
11: After continuation 10
13: After continuation 8
11: After continuation 7
11: After continuation 5
11: After continuation 4
11: After continuation 3
11: After continuation 2
11: After continuation 1
13: After continuation 6
12: After continuation 9
== Wait long-running task
No Sync context
15: Before continuation 1
15: Before continuation 2
15: Before continuation 3
15: Before continuation 4
15: Before continuation 5
15: Before continuation 6
15: Before continuation 7
15: Before continuation 8
15: Before continuation 9
15: Before continuation 10
=== Start
Sync context hash code: 57352375
== Wait
Sync context hash code: 57352375
9: Before continuation 1
9: Before continuation 2
9: Before continuation 3
9: Before continuation 4
9: Before continuation 5
9: Before continuation 6
9: Before continuation 7
9: Before continuation 8
9: Before continuation 9
9: Before continuation 10
12: After continuation 4
13: After continuation 2
12: After continuation 1
11: After continuation 3
14: After continuation 5
13: After continuation 1
13: After continuation 7
13: After continuation 6
14: After continuation 8
11: After continuation 10
12: After continuation 9
13: After continuation 10
11: After continuation 9
11: After continuation 6
11: After continuation 5
11: After continuation 4
13: After continuation 3
12: After continuation 8
11: After continuation 2
14: After continuation 7
== Wait long-running task
No Sync context
17: Before continuation 1
17: Before continuation 2
17: Before continuation 3
17: Before continuation 4
17: Before continuation 5
17: Before continuation 6
17: Before continuation 7
17: Before continuation 8
17: Before continuation 9
17: Before continuation 10
11: After continuation 10
16: After continuation 9
16: After continuation 4
16: After continuation 3
16: After continuation 2
12: After continuation 7
14: After continuation 8
16: After continuation 1
11: After continuation 5
13: After continuation 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment