2012-11-29 4 views
0

이 부분을 바탕으로 question 나는 Jim Mischel 권장 사항을 기반으로 솔루션에 waithandles/eventwaithandle을 사용하기로 결정했습니다. 나는 거의 "일하고있다. 코드는 다음과 같습니다다중 프로세스 및 대기 핸들

Private Sub InitDeploymentCheck() 
moDeploymentCheck = New TRS.Deployment.TRSDeploymentCheck(EnvironmentVariables.Environment, AppDomain.CurrentDomain.BaseDirectory.Contains("bin"), MDIMain) 
AddHandler moDeploymentCheck.DeploymentNeeded, 
    Sub() 
     moTimer = New System.Windows.Forms.Timer() 
     moTimer.Interval = 300000 '5 minutes 
     moTimer.Enabled = True 
     AddHandler moTimer.Tick, 
      Sub() 
       'check to see if the message box exist or not before throwing up a new one 

       'check to see if the wait handle is non signaled, which means you shouldn't display the message box 
       If waitHandle.WaitOne(0) Then 
        'set handle to nonsignaled 
        waitHandle.Reset() 
        MessageBox.Show(MDIMain, "There is a recent critical deployment, please re-deploy STAR to get latest changes.", "Critical Deployment", MessageBoxButtons.OK, MessageBoxIcon.Warning) 
        'set the handle to signaled 
        waitHandle.Set() 
       End If 


      End Sub 
     waitHandle.Set() 
     MessageBox.Show(MDIMain, "There is a recent critical deployment, please re-deploy STAR to get latest changes.", "Critical Deployment", MessageBoxButtons.OK, MessageBoxIcon.Warning) 
    End Sub 
End Sub 

다시 말하지만 거의 모든 우리 애플리케이션에서 상속받은 기본 폼의 것입니다. 우리가 하나의 앱을 사용할 때 완벽하게 작동합니다. 기본 양식을 상속하고 누군가가 메시지 상자 중 하나를 클릭하면 여러 개의 응용 프로그램을 실행하는 경우 두 번째 메시지 상자가 다른 응용 프로그램에 표시되는 경우가 있습니다. 나는 원래 waithandle을 정적/공유로 ​​선언하고 그것이 문제라고 생각했지만 그렇지 않았습니다. 나 또한 각 응용 프로그램에 자체 waithandle을 만들고 기본으로 전달하려고 시도하고 동일한 결과가 발생했습니다. 다른 응용 프로그램간에 waithandle이 공유되는 이유는 누구입니까? WaitHandle은 실제로 ManualResetEvent입니다.

+0

어떻게 waitHandle을 만드십니까? – usr

+0

@usr : 클래스 상단의 전용 변수입니다. Dim waitHandle as New ManualResetEvent (True) – coding4fun

답변

1

먼저 여러 응용 프로그램에서이 메서드를 사용하려면 this constructor을 사용하여 EventWaitHandle이라는 이름을 지정하거나 명명 된 개체를 만드는 다른 메서드 중 하나를 만들어야합니다. ManualResetEvent은 단일 프로세스에서만 작동합니다.

둘째로, Mutex이 더 나은 해결책 일 수 있습니다. 방금 권장하는 코드에는 경쟁 조건이 있다는 것을 알게되었습니다. 스레드 A가 WaitOne(0)을 수행하고 성공하고 스레드 B가 들어오고 스레드 A가 Reset을 호출하기 전에 동일한 작업을 수행하면 두 스레드 모두 메시지 상자를 표시합니다.

MutexWaitOne(0)을 사용하면이 문제를 해결할 수 있습니다. 그래도 Mutex 해제해야합니다 : 그것은 제대로 작동되지

if (mutex.WaitOne(0)) 
{ 
    try 
    { 
     // do stuff 
    } 
    finally 
    { 
     mutex.ReleaseMutex(); 
    } 
} 
0

이유는 내가 타이머 이벤트 외부의 첫 번째 메시지 상자를 보여주는 된 버그를 가지고 있다는 것입니다. 다음과 같아야합니다 :

waitHandle.Reset() 
MessageBox.Show(MDIMain, "There is a recent critical deployment, please re-deploy STAR to get latest changes.", "Critical Deployment", MessageBoxButtons.OK, MessageBoxIcon.Warning) 
waitHandle.Set()