Created
February 22, 2015 18:39
-
-
Save tugberkugurlu/cc1dd89020b32e58f54b to your computer and use it in GitHub Desktop.
LogicalSetDataSample sample: CallContext.LogicalSetData and CallContext.LogicalGetData
This file contains hidden or 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.Linq; | |
using System.Runtime.Remoting.Messaging; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace LogicalSetDataSample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Task.WaitAll(Enumerable.Range(1, 10).Select(async i => | |
{ | |
await FirstAsync(i, 100*i); | |
}).ToArray()); | |
Console.ReadLine(); | |
} | |
static async Task FirstAsync(int message, int wait) | |
{ | |
Console.WriteLine("Thread: {0}, message: SET '{1}'", Thread.CurrentThread.ManagedThreadId, message); | |
CallContext.LogicalSetData("message", message); | |
await Task.Yield(); | |
await Task.Delay(300* message); | |
for (int i = 0; i < 10; i++) | |
{ | |
await WaitAndWriteAsync(wait, message.ToString()); | |
} | |
} | |
static async Task WaitAndWriteAsync(int waitAmount, string expectedMessage) | |
{ | |
await Task.Delay(waitAmount); | |
Console.WriteLine("Thread: {0}, Expected: {1}, Message: {2}", Thread.CurrentThread.ManagedThreadId, expectedMessage, CallContext.LogicalGetData("message")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment