InvokeRequired返回尽管通话假是不是从MainThreadInvokeRequired、MainThread

由网友(你是我不屑的忧伤)分享简介:在VB.Net,我上有一个复选框形式 - 如被选中,我想消息出现弹出窗口,如果不加以控制消息是燮pressed。 In VB.Net, I have a form with a checkbox on it - if checked, I want messages to appear as popups, if u...

在VB.Net,我上有一个复选框形式 - 如被选中,我想消息出现弹出窗口,如果不加以控制消息是燮pressed。

In VB.Net, I have a form with a checkbox on it - if checked, I want messages to appear as popups, if unchecked messages are to be suppressed.

Partial Class mainForm

   ...

Public Delegate Sub dlgShowPopup(xString As String)
Public Sub showPopup(xStrMessage As String)
    If (Me.InvokeRequired) Then
        Dim xDlg As New dlgShowPopup(AddressOf showPopup)
        Me.Invoke(xDlg, xStrMessage)
    Else
        If (Me.cbShowPopup.Checked() = True) Then
            Me.notifyIcon.Visible = True
            Me.notifyIcon.ShowBalloonTip(1000, "VdtServer", xStrMessage, ToolTipIcon.Info)
        End if 
    End If
End Sub

   ...

End Class

但问题是,即使在情况下,它不会检查 Me.cbShowPopup.Checked()返回true。在调查过程中,我发现了 Me.InvokeRequired 从不返回false,即使调用线程不是MainThread,但的WorkerThread。到目前为止,我认为主要形式生活在MainThread,并从不同的线程调用时,Me.InvokeRequired应该解雇真。

Problem is Me.cbShowPopup.Checked() returns true even in case it is not checked. During investigations, I found out that the Me.InvokeRequired never returns false, even when the calling thread is not the MainThread, but WorkerThread. So far, I thought that the main form lives in the MainThread and when called from a different thread, Me.InvokeRequired should fire true.

推荐答案

Me.cbShowPop.Checked 是独立于调用的,所以我不能真正理解它是如何可以返回true未选中时,除非你有多种形式正在创建同一类型的。

Me.cbShowPop.Checked is independent of the Invoke, so I can't really understand how it can return true when it is not checked, unless you have multiple forms of the same type being created.

您的code可以无需显式代表这样可以简化

Your code can be simplified without the need for the explicit delegate like this:

Private Sub showPopup(xString As String)
    If Me.InvokeRequired Then
        Me.BeginInvoke(New Action(Of String)(AddressOf showPopup), xString)
    Else
        If Me.cbShowPopup.Checked Then
            Me.NotifyIcon1.Visible = True
            Me.NotifyIcon1.ShowBalloonTip(1000, "VdtServer", xString, ToolTipIcon.Info)
        End If
    End If
End Sub

另外请注意它不被认为是很好的做法,命名控制相同的名称作为其类,即你不应该叫的NotifyIcon 控制的NotifyIcon

阅读全文

相关推荐

最新文章