Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Uses Synchronization Contexts in the Windows form

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Feb 2011CPOL 7.8K   2  
Synchronization
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); // We can't be preempted here
Console.WriteLine ("end"); // thanks to automatic locking!
}
}
public class Test
{
public static void Main()
{
AutoLock safeInstance = new AutoLock();
new Thread (safeInstance.Demo).Start(); // Call the Demo
new Thread (safeInstance.Demo).Start(); // method 3 times
safeInstance.Demo(); // concurrently.
}
}


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).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
-- There are no messages in this forum --