如何从工作线程更新tabControl

时间:2017-08-15 19:49:36

标签: .net vb.net multithreading tabs thread-safety

我正在开发一个使用通过串口通信的比例的应用程序。当比例发送通信时,通信接收方法在与主/ UI线程不同的线程上执行。我需要更改tabControl的哪个选项卡在我从比例获得响应后显示。

我继续得到一个System.InvalidOperationException:'在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。'

缩放有一个方法应该(非常)调用主窗体来更新其选项卡显示,但我继续得到错误。

比例调用WorkOrder方法。

Private Sub Port_DataReceived(ByVal sender As System.Object, ByVal e As SerialDataReceivedEventArgs) Handles _port.DataReceived


            Dim inBuff As String = _port.ReadLine()
            Me.StopListeningToScale()

            Dim deleg

            deleg = New RecieveReferenceWeight(AddressOf Views.Main.Session.Order.RecieveReferenceWeight)

            inBuff = GetWeightValue(inBuff)

            deleg.Invoke(inBuff)

    End Sub

Private Delegate Sub RecieveReferenceWeight(ByVal referenceWeight As Double)

WorkOrder调用main_View来更新UI。

Public Sub RecieveReferenceWeight(ByVal referenceWeightValue As Double) Implements IOrder.RecieveReferenceWeight

        Me.ReferenceWeight = referenceWeightValue 'Set the reference weight.

        Dim deleg

        deleg = New GotReferenceWeight(AddressOf Views.Main.GotReferenceWeight)

        OrderScale.ClosePort() 'Close the port.
        OrderScale.selfDestruct() 'Destroy the scale object.

        deleg.Invoke()

    End Sub
    Private Delegate Sub GotReferenceWeight()

最后调用main_View方法,以便它可以更新它的选项卡控件。

Public Sub GotReferenceWeight()
            'This method advances the workflow tabs on the main view when we get the reference weight

            Procedure_TabDisplay.BeginInvoke(New MyDelegate(AddressOf DelegateMethod))

            Session.StartPacking()

        End Sub

        Delegate Sub MyDelegate(myControl As TabControl)

        Public Sub DelegateMethod(myControl As TabControl)
            myControl.SelectedIndex = 2
            Me.Controls.Add(myControl)
        End Sub

我想知道如何回到主线程。我已经尝试使用委托,事件,线程和control.beginInvoke但我继续得到类似的消息。任何解决方案都将不胜感激。

0 个答案:

没有答案