如何让ShowDialog()不阻止调用者的事件

时间:2015-04-07 05:31:16

标签: vb.net show rfid showdialog

我的程序只是一个小问题。我一直在VS2005 VB.Net中的设备应用程序中工作。该程序将在我的设备上运行,该设备将连接到蓝牙设备。这是当前的代码:

在处理事件的类中:

Public Class BluetoothDevice : Implements IRFID
    'static instance of class
    Public Shared _btDevice as BluetoothDevice
    Private WithEvents bluetoothDevice as BRIReader
    'This is the event handler
    Private Sub BluetoothDevice_EventHandlerConnectionStateChanged(ByVal sender as Object, ByVal EvtArgs As EVTADV_DeviceConnectionStateEventArgs) handles bluetoothDevice.EventHandlerDeviceConnectState
        Select Case EvtArgs.DeviceConnectState
            Case EVTADV_DeviceConnectionStateEventArgs.CONNECTED
                RaiseEvent OnReaderConnectionEventChanged(connectionState.Connected)
            Case EVTADV_DeviceConnectionStateEventArgs.OFFLINE
                RaiseEvent OnReaderConnectionEventChanged(connectionState.Offline)
            Case EVTADV_DeviceConnectionStateEventArgs.RECONNECTING
                RaiseEvent OnReaderConnectionEventChanged(connectionState.Reconnecting)
        End Select
    End Sub
End Class

在捕获RaiseEvent的UI表单中。这也是主要表格:

Private Delegate Sub xOnReaderConnectEventChangeHandler(ByVal connState as connectionState)
Private Sub OnReaderConnectEventChangeHandler(ByVal connState as connectionState)
    If Me.InvokeRequired Then
        Me.Invoke(New xOnReaderConnectEventChangeHandler(AddressOf OnReaderConnectEventChangeHandler), connState)
    Else
        Select Case connState
            Case connectionState.Connected
                '_form here is a global object containing the form we ShowDialog()
                If _form IsNot Nothing
                    _form.Dispose()
                    _form = Nothing
                EndIf
            Case connectionState.Offline
                'haven't done anything yet
            Case connectionState.Reconnecting
                'show the form
                _form.ShowDialog()
        End Select
    End If
End Sub

现在,这是尝试重新连接设备的表单。这是计算自重新连接状态以来经过的时间的形式,并且具有"取消"按钮:

Public Class FormReconnect
    Private WithEvents _irfid as IRFID

    Private Sub FormReconnect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'form load
        'this method returns the instance of BluetoothDevice Class
        _irfid = BluetoothDevice.GetInstance()
    End Sub

    'I also handle the event here since the caller form is not active
    'This form SHOULD close when the connection is established
    Private Delegate Sub xOnReaderConnectionEventChangeHandler(ByVal connState As connectionState)
    Private Sub OnReaderConnectionEventChangeHandler(ByVal connState As connectionState) Handles _irfid.OnReaderConnectionEventChange
        If Me.InvokeRequired Then
            Me.Invoke(New xOnReaderConnectionEventChangeHandler(AddressOf OnReaderConnectionEventChangeHandler), connState)
        Else
            Select Case connState
                Case connState.Connected
                    'I only handle the connected state. If Device is connected, close this form
                    Me.Close()
            End Select
        End If
    End Sub
End Class

情况是,当主窗体显示时,我从设备中取出电池,它们的连接状态将变为离线状态,然后立即重新连接,然后显示重新连接表格。问题是,当通过ShowDialog()方法激活表单重新连接时,即使设备连接状态已经BluetoothDevice,也不会触发处理事件的类(Connected)。设备中显示闪烁的灯光)。似乎表单重新连接ShowDialog()正在阻止该类,因为当我按下取消按钮并关闭表单重新连接时,将触发BluetoothDevice的事件处理程序。这里最好的解决方法是什么?我已尝试Show(),它会触发事件,但它由调用者和表单重新连接处理,使表单重新连接关闭,而不显示主窗体。

编辑1:我希望这样做,当我ShowDialog()并且表单重新连接处于活动状态时(因此,表单主屏幕为"隐藏"在重新连接下),Form Reconnect将处理BluetoothDevice Class中引发的事件。

编辑2:当表单重新连接处于活动状态时。 BluetoothDevice不会引发任何事件。认为它被ShowDialog()阻止了。因此,即使连接状态为"已连接"。

,表单重新连接也不会关闭

2 个答案:

答案 0 :(得分:1)

使用此方法。

  1. 主表单(使用单例模式)处理与设备关联的所有事件
  2. 断开连接后,或者在开始时创建一个线程并将show reconnect表单(再次使用此表单的单例模式)逻辑放在那里。

    MainForm.getInstance.Invoke(New MethodInvoker(Sub()) Dim formReconnect As Form = formReconnect.GetInstance() 如果formReconnect.visible = False那么     formReconnect.TopMost = True     formReconnect.Show() 结束如果
    结束Sub))

  3. 通过重新连接窗口重新连接调用主表单并关闭重新连接表单

  4. 如果设备自动重新连接,主表单会在重新连接表单上调用close方法。

答案 1 :(得分:0)

在提升表单重新连接时使用BeginInvoke,这样,表单重新连接可以处理事件。我只需使用RemoveHandler Form Main来防止它引发相同的事件。