문제

I have a C# Windows application I'm working on where I kick off different classes in their own thread (ie: Class1 instance in Thread 1, Class 2 instance in Thread 2, etc).

I'm trying to correctly suspend/resume the threads, but I can't figure out how to share the lock variables for Wait/Pulse of Monitor between different classes and threads.

It seems like this should have a simple answer, I just can't figure it out. I'd really appreciate any help!

Thanks!

도움이 되었습니까?

해결책

use public static fields, for ex.,

public class Sync
{
    public static object LockObject = new object();
    //or any other sync mechanism 
    //AutoResetEvent,ManualResetEvent,Semaphore,CountdownEvent,Mutex etc.
}

다른 팁

Monitor locking is useful for shnchronization, but when used as a messaging API it usually only scales to two threads, since you need to know exactly where each to know if it gets the message.

When things get complex, an AutoResetEvent or ManualResetEvent is usually more sensible, as they are less time critical: you get through the gate regardless of ordering.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top