Skip to content

Instantly share code, notes, and snippets.

@samoshkin
Created November 2, 2011 17:21
Show Gist options
  • Save samoshkin/1334279 to your computer and use it in GitHub Desktop.
Save samoshkin/1334279 to your computer and use it in GitHub Desktop.
Call context with child threads
CallContext.SetData("key", new Number { N = 1 });
var task1 = Task.Factory.StartNew(() =>
{
var n = CallContext.GetData("key") as Number;
n.N++;
Thread.Sleep(1000);
});
var task2 = Task.Factory.StartNew(() =>
{
var n = CallContext.GetData("key") as Number;
n.N++;
Thread.Sleep(1000);
});
Task.WaitAll(task1, task2);
LoggingUtils.TraceMessageF("Result number: {0}", (CallContext.GetData("key") as Number).N);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment