vb.net 在主线程上使用句柄运行 Sub
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17513969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Sub with handle on main thread
提问by sharkyenergy
I have a function that is executed. This has some events that fire some Subs.
我有一个被执行的函数。这有一些事件会触发一些 Subs。
The function runs on a different thread then the main thread. Problem I have with this is that the subs connected with the event are also called on a different thread. I would need these to be called on the main thread so I can access the interface.
该函数在与主线程不同的线程上运行。我遇到的问题是与事件相关的 subs 也在不同的线程上调用。我需要在主线程上调用这些,以便我可以访问接口。
Is this possible?
这可能吗?
UPDATE
更新
code sample, this code is executed on the main thread
代码示例,这段代码在主线程上执行
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)
the very last line calls a function, this is executed on a different thread and has some events. these events fire following sub on the different thread, I would need them on the main thread.
最后一行调用一个函数,它在不同的线程上执行并有一些事件。这些事件在不同的线程上跟随 sub 触发,我需要它们在主线程上。
Private Sub Upload_UploadFileCompleted(ByVal sender As Object, ByVal e As UploadFileCompletedEventLibArgs) Handles ftpUpload.UploadFileCompleted
'dothiings with the interface
End Sub
采纳答案by Yvette
This link gives a good answer on how to create a delegate and then call it back to the main thread.
此链接提供了有关如何创建委托然后将其回调到主线程的很好的答案。
vb.net threading.thread addressof passing variables
vb.net threading.thread 传递变量的地址
Another good generic example:
另一个很好的通用示例:
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/
http://tech.xster.net/tips/invoke-ui-changes-across-threads-on-vb-net/
As you have provided no code, it is impossible for me to customise my answer to suit your needs. Instead I have copied a generic solution from MSDN.
由于您未提供任何代码,因此我无法根据您的需要自定义我的答案。相反,我从 MSDN 复制了一个通用解决方案。
Threading can be used in different ways, in vb.net the GUI runs on one thread, so many processes need to be handled on a separate thread to stop the GUI from locking up.
线程可以以不同的方式使用,在 vb.net 中,GUI 在一个线程上运行,因此需要在单独的线程上处理许多进程以阻止 GUI 锁定。
There are many permutations for achieving this, however this code and the links provided from this page, will give you all the information you need to at least get started.
有很多排列可以实现这一点,但是此代码和此页面提供的链接将为您提供至少入门所需的所有信息。
If you cannot get your code working, please feel free to ask another, more specific question showing your code, and I and/or others, would happily assist you.
如果您的代码无法正常工作,请随时提出另一个显示您的代码的更具体的问题,我和/或其他人将很乐意为您提供帮助。
The example from MSDN.
来自 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
See Thread Class:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx
请参阅线程类:http:
//msdn.microsoft.com/en-us/library/system.threading.thread.aspx

