Created
November 26, 2014 13:45
-
-
Save StephenCleary/5aaed00d8452bf5a4541 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
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