There is a way to allow windows form of Synchronization Contexts. Use code like this:
using System;
using System.Threading;
using System.Runtime.Remoting.Contexts;
[Synchronization]
public class AutoLock : ContextBoundObject
{
public void Demo()
{
Console.Write ("Start...");
Thread.Sleep (1000);
Console.WriteLine ("end");
}
}
public class Test
{
public static void Main()
{
AutoLock safeInstance = new AutoLock();
new Thread (safeInstance.Demo).Start();
new Thread (safeInstance.Demo).Start();
safeInstance.Demo();
}
}
According to this point:
Automatic synchronization cannot be used to protect static type members, nor classes not derived from
ContextBoundObject
(for instance, a Windows Form).