2

我有一个执行的功能。这有一些事件会触发一些 Subs。

该函数在与主线程不同的线程上运行。我遇到的问题是与事件连接的潜艇也在不同的线程上被调用。我需要在主线程上调用这些,以便我可以访问界面。

这可能吗?

更新

代码示例,此代码在主线程上执行

 Dim url As String = Ftp


If Not url.StartsWith("ftp://") Then url = "ftp://" & url

Dim uri As New Uri(url)

ftpUpload.Host = uri.Host
ftpUpload.UserName = Benutzername
ftpUpload.Password = Passwort
ftpUpload.Port = 21
Dim localDirectory = Path.GetDirectoryName(Datei) & "\"
Dim localFilenameUpload = Path.GetFileName(Datei)
Dim remoteDirectory = uri.AbsolutePath
Dim remoteFilename = Path.GetFileName(Datei)
ftpUpload.UploadAsync(localDirectory, localFilenameUpload, remoteDirectory, remoteFilename)

最后一行调用了一个函数,它在不同的线程上执行并且有一些事件。这些事件在不同线程上触发,我需要它们在主线程上。

Private Sub Upload_UploadFileCompleted(ByVal sender As Object, ByVal e As UploadFileCompletedEventLibArgs) Handles ftpUpload.UploadFileCompleted

'dothiings with the interface

End Sub
4

1 回答 1

1

这个链接很好地回答了如何创建一个委托,然后将其回调到主线程。

vb.net threading.thread 传递变量的地址

另一个很好的通用示例:

thread = New System.Threading.Thread(AddressOf DoStuff)
thread.Start()
Private Delegate Sub DoStuffDelegate()
Private Sub DoStuff()
    If Me.InvokeRequired Then
        Me.Invoke(New DoStuffDelegate(AddressOf DoStuff))
    Else
        Me.Text = "Stuff"
    End If
End Sub

http://tech.xster.net/tips/invoke-ui-changes-across-threads-on-vb-net/

由于您没有提供代码,因此我无法根据您的需要定制我的答案。相反,我从 MSDN 复制了一个通用解决方案。

线程可以以不同的方式使用,在 vb.net 中,GUI 在一个线程上运行,因此需要在单独的线程上处理许多进程以阻止 GUI 锁定。

实现这一点有许多排列方式,但是此代码和此页面提供的链接将为您提供至少入门所需的所有信息。

如果您的代码无法正常工作,请随时提出另一个更具体的问题来显示您的代码,我和/或其他人会很乐意为您提供帮助。

来自 MSDN 的示例。

Imports System
Imports System.Threading

' Simple threading scenario:  Start a Shared method running 
' on a second thread. 
Public Class ThreadExample
    ' The ThreadProc method is called when the thread starts. 
    ' It loops ten times, writing to the console and yielding  
    ' the rest of its time slice each time, and then ends. 
    Public Shared Sub ThreadProc()
        Dim i As Integer 
        For i = 0 To 9
            Console.WriteLine("ThreadProc: {0}", i)
            ' Yield the rest of the time slice.
            Thread.Sleep(0)
        Next 
    End Sub 

    Public Shared Sub Main()
        Console.WriteLine("Main thread: Start a second thread.")
        ' The constructor for the Thread class requires a ThreadStart  
        ' delegate.  The Visual Basic AddressOf operator creates this 
        ' delegate for you. 
        Dim t As New Thread(AddressOf ThreadProc)

        ' Start ThreadProc.  Note that on a uniprocessor, the new  
        ' thread does not get any processor time until the main thread  
        ' is preempted or yields.  Uncomment the Thread.Sleep that  
        ' follows t.Start() to see the difference.
        t.Start()
        'Thread.Sleep(0) 

        Dim i As Integer 
        For i = 1 To 4
            Console.WriteLine("Main thread: Do some work.")
            Thread.Sleep(0)
        Next

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.")
        t.Join()
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.")
        Console.ReadLine()
    End Sub 
End Class

请参阅线程类:http:
//msdn.microsoft.com/en-us/library/system.threading.thread.aspx

于 2013-07-07T16:50:07.310 回答