vb.net 如何将此函数放在单独的线程中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13603606/
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
How to put this function inside a separate thread
提问by ElektroStudios
I need a little (or big) help with my form: I need to use everything inside the "Organize function" region in a separate thread.
我的表单需要一点(或大)帮助:我需要在单独的线程中使用“组织功能”区域内的所有内容。
I press a button in my form's "Start button" region to call the first sub of the "Organize function" subs; the first sub calls the second sub and the second sub calls the third sub.
我按下表单“开始按钮”区域中的一个按钮来调用“组织功能”子程序的第一个子程序;第一个子调用第二个子,第二个子调用第三个子。
I tried adding the third sub into a separate thread by myself and then using the second sub to pass the argument to the thread but all I've done is wrong.
我尝试自己将第三个 sub 添加到一个单独的线程中,然后使用第二个 sub 将参数传递给该线程,但我所做的一切都是错误的。
Can someone please help me do this?
有人可以帮我做这个吗?
PS: I've deleted the non-important parts in this form to let you check better.
PS:我把这个表格里不重要的部分删掉了,方便大家检查。
Thank you for reading.
感谢您的阅读。
Public Class Form1
#Region "Declarations"
' MediaInfo
Dim MI As MediaInfo
' Thread
Dim paused As Boolean = False
' Others
Dim NameOfDirectory As String = Nothing
Dim aFile As FileInfo
#End Region
'thread
Dim t As New Thread(AddressOf ThreadProc)
Public Sub ThreadProc()
' Aqui debería ir todo el sub de "organize function", bueno... son 3 subs!
If paused = True Then MsgBox("THREAD PAUSADO")
End Sub
#Region "Properties"
...
#End Region
#Region "Load / Close"
...
#End Region
#Region "Get Total files Function"
...
#End Region
#Region "Option checkboxes"
...
#End Region
#Region "Folder buttons"
...
#End Region
#Region "Append text function"
...
#End Region
#Region "Action buttons"
' pause button
Private Sub pause_button_Click(sender As Object, e As EventArgs) Handles pause_button.Click
paused = True
End Sub
' start button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles start_button.Click
t.Start()
' Organization process
NameOfDirectory = userSelectedFolderPath
MediaInfo(NameOfDirectory)
End Sub
#End region
#Region "Organize function"
Public Sub MediaInfo(Directory)
Dim MyDirectory As DirectoryInfo
MyDirectory = New DirectoryInfo(NameOfDirectory)
MediaInfoWorkWithDirectory(MyDirectory)
End Sub
Public Sub MediaInfoWorkWithDirectory(ByVal aDir As DirectoryInfo)
Dim nextDir As DirectoryInfo
MediaInfoWorkWithFilesInDir(aDir)
For Each nextDir In aDir.GetDirectories
Using writer As StreamWriter = New StreamWriter(aDir.FullName & "\" & nextDir.Name & "\" & nextDir.Name & ".m3u", False, System.Text.Encoding.UTF8)
'overwrite existing playlist
End Using
MediaInfoWorkWithDirectory(nextDir)
Next
End Sub
Public Sub MediaInfoWorkWithFilesInDir(ByVal aDir As DirectoryInfo)
Dim aFile As FileInfo
For Each aFile In aDir.GetFiles()
' hacer cosas con aFile ...
Next
End Sub
#End Region
End Class
回答by Teppic
There is a Windows Forms component called BackgroundWorker that is designed specifically to offload long-running tasks from the UI thread to a background thread, leaving your form nice and responsive.
有一个名为 BackgroundWorker 的 Windows 窗体组件,专门设计用于将长时间运行的任务从 UI 线程卸载到后台线程,使您的窗体保持良好且响应迅速。
The BackgroundWorker component has an event called DoWork that is used to execute code on a separate thread. Drag the BackgroundWorker component onto your form and then do something like this:
BackgroundWorker 组件有一个名为 DoWork 的事件,用于在单独的线程上执行代码。将 BackgroundWorker 组件拖到您的表单上,然后执行如下操作:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles start_button.Click
NameOfDirectory = userSelectedFolderPath
backgroundWorker1.RunWorkerAsync(NameOfDirectory)
End Sub
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim directoryName as string = e.Argument
MediaInfo(directoryName)
End Sub
A couple of links that might be useful are the MSDN BackgroundWorkerpage and an example on Code Project.
一些可能有用的链接是MSDN BackgroundWorker页面和Code Project上的示例。
HTH
HTH
回答by igrimpe
There are around 5 dozen ways to solve the problem. I will show just 3 of them:
解决问题的方法大约有 5 种。我将只展示其中的 3 个:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' fire and forget:
Task.Run(Sub() FooA()).ContinueWith(Sub() FooB()).ContinueWith(Sub() FooC())
Console.WriteLine("Button1 done")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' fire and forget:
Task.Run(Sub()
FooA()
FooB()
FooC()
End Sub)
Console.WriteLine("Button2 done")
End Sub
Private Async Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
' wait but dont block:
Await Task.Run(Sub()
FooA()
FooB()
FooC()
End Sub)
Console.WriteLine("Button3 done")
End Sub
Private Sub FooA()
Threading.Thread.Sleep(1000)
Console.WriteLine("A")
End Sub
Private Sub FooB()
Threading.Thread.Sleep(1000)
Console.WriteLine("B")
End Sub
Private Sub FooC()
Threading.Thread.Sleep(1000)
Console.WriteLine("C")
End Sub
End Class
I would suggest the one with Await(IF FW 4.x and VS2012 is not an issue).
我建议使用Await(如果 FW 4.x 和 VS2012 不是问题)。

