Skip to content

Instantly share code, notes, and snippets.

@StephenCleary
Created November 26, 2014 13:45
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 StephenCleary/5aaed00d8452bf5a4541 to your computer and use it in GitHub Desktop.
Save StephenCleary/5aaed00d8452bf5a4541 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
int threadId = Task.Run(() =>
{
CallContext.LogicalSetData("k", 0);
Console.WriteLine("Write logical data on thread " + Thread.CurrentThread.ManagedThreadId);
return Thread.CurrentThread.ManagedThreadId;
}).Result;
while (true)
{
var tcs = new TaskCompletionSource<Tuple<int, object>>();
Task.Run(() =>
{
tcs.SetResult(Tuple.Create(Thread.CurrentThread.ManagedThreadId, CallContext.LogicalGetData("k")));
if (Thread.CurrentThread.ManagedThreadId != threadId)
Thread.Sleep(3000);
});
var result = tcs.Task.Result;
if (result.Item1 == threadId)
{
Console.WriteLine("Read logical data from thread " + result.Item1 + ": " + result.Item2);
break;
}
}
Console.ReadKey();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment