Last active
August 29, 2015 13:56
-
-
Save yallie/9323777 to your computer and use it in GitHub Desktop.
LogicalCallContext bug in Mono 3.2.x
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
Original thread: 1 | |
Current thread: 3 | |
Value is null | |
Current thread: 5 | |
Value is null | |
Current thread: 5 | |
Value is null |
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
Original thread: 1 | |
Current thread: 3 | |
Value: Hello World | |
Current thread: 4 | |
Value: Hello World | |
Current thread: 5 | |
Value: Hello World |
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
using System; | |
using System.Runtime.Remoting.Contexts; | |
using System.Runtime.Remoting.Messaging; | |
using System.Threading; | |
using System.Threading.Tasks; | |
public class Holder : ILogicalThreadAffinative { | |
public string Value { get; set; } | |
} | |
class Program { | |
const string SlotName = "Test"; | |
static void Main() { | |
Console.WriteLine("Original thread: {0}", Thread.CurrentThread.ManagedThreadId); | |
CallContext.SetData(SlotName, new Holder { | |
Value = "Hello World" | |
}); | |
new Thread(() => { | |
Console.WriteLine("Current thread: {0}", Thread.CurrentThread.ManagedThreadId); | |
var h = CallContext.GetData(SlotName) as Holder; | |
if (h != null) | |
Console.WriteLine("Value: {0}", h.Value); | |
else | |
Console.WriteLine("Value is null"); | |
}).Start(); | |
ThreadPool.QueueUserWorkItem(x => { | |
Console.WriteLine("Current thread: {0}", Thread.CurrentThread.ManagedThreadId); | |
var h = CallContext.GetData(SlotName) as Holder; | |
if (h != null) | |
Console.WriteLine("Value: {0}", h.Value); | |
else | |
Console.WriteLine("Value is null"); | |
}); | |
Task.Factory.StartNew(() => { | |
Console.WriteLine("Current thread: {0}", Thread.CurrentThread.ManagedThreadId); | |
var h = CallContext.GetData(SlotName) as Holder; | |
if (h != null) | |
Console.WriteLine("Value: {0}", h.Value); | |
else | |
Console.WriteLine("Value is null"); | |
}); | |
Thread.Sleep(300); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment