简体   繁体   English

在 VB.net 中使用线程

[英]Using threading in VB.net

I'm having trouble using threading in a VB.net application I'm writing.我在正在编写的 VB.net 应用程序中使用线程时遇到问题。 I have a really simple version here to illustrate the problem I'm having.我在这里有一个非常简单的版本来说明我遇到的问题。

It works perfectly if I load my form and have the VNC control connect to my VNC server as a single thread application using this code:如果我加载表单并使用以下代码将 VNC 控件作为单线程应用程序连接到我的 VNC 服务器,它会完美运行:

Imports VncSharp
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not RemoteDesktop1.IsConnected Then
            Dim Host As String = "10.0.0.1"
            RemoteDesktop1.Connect(Host)
        End If
    End Sub
End Class

In order to make the connection occur in the background, this is the code I've tried using the backgroundworker control.为了使连接在后台发生,这是我尝试使用 backgroundworker 控件的代码。 I followed the information in this article to handle referencing the controls on the form using an instance, but I still get an error when it tries to use the VNCSharp control (RemoteDesktop1) to connect to a VNC server at the line: Instance.RemoteDesktop1.Connect(Host)我按照本文中的信息使用实例处理引用表单上的控件,但是当它尝试使用 VNCSharp 控件 (RemoteDesktop1) 连接到以下行的 VNC 服务器时,我仍然收到错误: Instance.RemoteDesktop1。连接(主机)

The error is:错误是:

System.InvalidOperationException: 'Cross-thread operation not valid: Control 'RemoteDesktop1' accessed from a thread other than the thread it was created on.' System.InvalidOperationException:“跨线程操作无效:控件“RemoteDesktop1”从创建它的线程以外的线程访问。

Here's the code:这是代码:

Imports VncSharp
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Form1
    
    'Taken from https://stackoverflow.com/questions/29422339/update-control-on-main-form-via-background-worker-with-method-in-another-class-v
    Private Shared _instance As Form1
    Public ReadOnly Property Instance As Form1
        Get
            Return _instance
        End Get
    End Property

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        _instance = Me
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        System.Threading.Thread.Sleep(1000)
        If Not Instance.RemoteDesktop1.IsConnected Then
            Dim Host As String = "10.0.0.1"
            Instance.RemoteDesktop1.Connect(Host)
            'Connect()
        End If
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        MsgBox("BG1 complete")
    End Sub
End Class

I figured it out - one part of the code in the post I linked to on threading held the answer: I needed to use Invoke() to reference the form RemoteDesktop control:我想通了——我在线程上链接到的帖子中的一部分代码给出了答案:我需要使用Invoke()来引用表单 RemoteDesktop 控件:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
  System.Threading.Thread.Sleep(1000)
    If Not Instance.RemoteDesktop1.IsConnected Then
      Me.Instance.Invoke(Sub()
        Dim Host As String = "10.61.41.7"
        Me.RemoteDesktop1.Connect(Host)
      End Sub)
  End If
End Sub

instead of just putting Instance.RemoteDesktop1.Connect(Host)而不是仅仅放置Instance.RemoteDesktop1.Connect(Host)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM