Skip to content

Instantly share code, notes, and snippets.

@yallie
Last active August 29, 2015 13:56
Show Gist options
  • Save yallie/9323777 to your computer and use it in GitHub Desktop.
Save yallie/9323777 to your computer and use it in GitHub Desktop.
LogicalCallContext bug in Mono 3.2.x
Original thread: 1
Current thread: 3
Value is null
Current thread: 5
Value is null
Current thread: 5
Value is null
Original thread: 1
Current thread: 3
Value: Hello World
Current thread: 4
Value: Hello World
Current thread: 5
Value: Hello World
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