如何将此函数放在单独的线程中 [英] How to put this function inside a separate thread

查看:87
本文介绍了如何将此函数放在单独的线程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上需要一点(或很大)帮助:我需要在单独的线程中使用组织功能"区域内的所有内容.

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.

有人可以帮我吗?

PS:我已删除此表格中不重要的部分,以方便您检查.

PS: I've deleted the non-important parts in this form to let you check better.

感谢您的阅读.

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

推荐答案

有一个名为BackgroundWorker的Windows窗体组件,该组件专门用于将长时间运行的任务从UI线程卸载到后台线程,使您的表单保持良好的响应速度

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组件具有一个称为DoWork的事件,该事件用于在单独的线程上执行代码.将BackgroundWorker组件拖到您的窗体上,然后执行以下操作:

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:

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

MSDN BackgroundWorker 页和代码项目上的示例

HTH

这篇关于如何将此函数放在单独的线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆