InvokeRequired返回尽管通话假是不是从MainThread [英] InvokeRequired returns False despite call is not from MainThread

查看:152
本文介绍了InvokeRequired返回尽管通话假是不是从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 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

这篇关于InvokeRequired返回尽管通话假是不是从MainThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆