
How to make static methods thread safe
You might want to look into the System.Threading.Mutex class as well.
Instance a mutex in you main class for each member (say, a collection) you
want to make thread safe.
System.Threading.Mutex m1 = new System.Threading.Mutex();
Then in each place where you access this collection, lock it using
m1.WaitOne();
and release it using:
m1.ReleaseMutex();
I believe you get more control over your locks and can optimize your apps
better.
--
Robert Jeppesen
MindCom AB
Quote:
> > I believe you could you use the lock keyword for this, like below:
> > class MyClass {
> > public static void MyThreadSafeFunc() {
> > lock(typeof(MyClass)) {
> > //my thread safe code here
> > }
> > }
> > }
> > The fact that I believe this means it's probably wrong, though...
> Sure not, it's completely correct.
> By setting the lock on a type, you effectively prevent other threads to
run the code inside in the block.
Quote: