Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 多流程&;等待手柄_.net_Vb.net_Event Wait Handle - Fatal编程技术网

.net 多流程&;等待手柄

.net 多流程&;等待手柄,.net,vb.net,event-wait-handle,.net,Vb.net,Event Wait Handle,基于此,我决定尝试使用waithandles/eventwaithandle作为基于Jim Mischel建议的解决方案。我“几乎”让它工作了。这是密码 Private Sub InitDeploymentCheck() moDeploymentCheck = New TRS.Deployment.TRSDeploymentCheck(EnvironmentVariables.Environment, AppDomain.CurrentDomain.BaseDirectory.Contains(

基于此,我决定尝试使用waithandles/eventwaithandle作为基于Jim Mischel建议的解决方案。我“几乎”让它工作了。这是密码

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声明为static/shared,并认为这是问题所在,但事实并非如此。我还尝试让每个应用程序创建自己的waithandle,并将其传递到base中,结果产生了相同的效果。有人知道为什么waithandle会在不同的应用程序之间共享吗?哦,顺便说一句,waitHandle实际上是一个ManualResetEvent

首先,如果你想在多个应用程序中使用它,你必须使用创建一个命名的
EventWaitHandle
或者其他创建命名对象的方法之一。
ManualResetEvent
仅适用于单个进程

第二,命名可能是更好的解决方案。我刚刚意识到我推荐的代码有一个竞争条件。如果线程A执行了
WaitOne(0)
并成功,然后线程B出现并在线程A调用
Reset
之前执行相同的操作,那么两个线程都将显示消息框

使用
Mutex
WaitOne(0)
将解决该问题。请务必释放互斥锁,不过:

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

它不能正常工作的原因是我有一个bug,在那里我显示了计时器事件之外的第一个消息框。应该是:

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

您是如何创建waitHandle的?@usr:只是类Dim waitHandle顶部的一个私有变量,即New ManualResetEvent(True)