在主线程上使用句柄运行Sub

时间:2013-07-07 16:29:46

标签: vb.net multithreading

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

该函数在主线程的不同线程上运行。我遇到的问题是与事件连接的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)

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

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

'dothiings with the interface

End Sub

1 个答案:

答案 0 :(得分:1)

此链接为如何创建委托然后将其调回主线程提供了一个很好的答案。

vb.net threading.thread addressof passing variables

另一个很好的通用例子:

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