모든 WaitOne 및 ManualResetEvent 작업이 작동합니다 (감사합니다!) 스레드에서 클래스 A의 함수를 실행하는 마지막 문제가 있습니다. 클래스 B의 일부인 - 다시 설명 할 수있게 해주세요 ...클래스 B의 스레드에서 클래스 A의 함수 호출 [C#]
클래스 A에서 "DoIt (obejct param)"함수를 살펴보면 클래스 A에서 (그림과 같이) 호출해야하며 클래스 B의 스레드 (그림 참조) ... 어떻게이 작업을 수행 할 수 있습니까? 일부 대표단이 도움이 될까요?
class A
{
private ManualResetEvent manualResetEvent;
int counter = 0;
public A()
{
manualResetEvent = new ManualResetEvent(false);
B child = new B(manualResetEvent);
if (manualResetEvent.WaitOne(1000, false))
{
... do the work I was waiting on ...
}
... do more work ...
// Call the function DoIt from within A //
DoIt(param)
}
// This is the function that needs to be called from A and thread in B
void DoIt(object param)
{
counter++;
... do something with param with is local to A ...
}
};
Class B
{
private ManualResetEvent manualResetEvent;
public B(ManualResetEvent mre)
{
manualResetEvent = mre;
Thread childThread = new Thread(new ThreadStart(Manage));
childThread.IsBackground = true;
childThread.Name = "NamedPipe Manager";
childThread.Start();
private void Manage()
{
... do some work ...
... call some functions ...
// Calling the function from Class A, obviouslly doesn't work as-is
DoIt(param);
manualResetEvent.Set();
... do more work ...
... call more functions ...
}
}
};
이 작업을 스레드로부터 안전하게 수행 할 수있는 방법에 대한 제안이 있습니까? 도움을 주시면 감사하겠습니다. 감사합니다,
"this"를 전달하고 parent.manualResetEvent를 대신 사용하는 것이 좋지 않겠습니까? 아마도 – Shaitan00
. 그러나 당신이 좋은 디자인에 관심이 있다면 아마도 그 작은 일뿐만 아니라 전체를 다시 평가해야합니다. – DSO