线程启动Vb.Net时挂起其他线程

时间:2014-09-24 14:57:37

标签: vb.net multithreading

我正在开发一个实用程序,它扫描用户定义的目录并提取所有文件扩展名 在下面提到的按钮事件中,我正在启动线程,其中所有扫描都已完成,但我希望代码暂停或停在“点A”,直到该线程完成其工作,而现在它启动线程并继续'点一个'。如何停止代码继续?

Private Sub Btn_Extract_Click(sender As Object, e As EventArgs) Handles Btn_Extract.Click
    Dim eventobject As New EventsandFunctions
    eventobject.FilesCounter = 0
    eventobject.DirectoryCounter = 0
    AddHandler eventobject.FilesScanned, AddressOf eventobject.FilesScannedCounterIncrease
    AddHandler eventobject.DirectoryScanned, AddressOf eventobject.DirectoryScannedCounterIncrease
    If TxtB_SaveFilePath.Text = Nothing Or TxtBx_DirectoryToScan.Text = Nothing Then
        MessageBox.Show("Scan Path, Output File Path or Both Missing", "Error")
    Else

        ThreadFileScanning.Start(eventobject) 'Thread Starts here--<Point A>
        If ChckB_RemoveDot.Checked Then
            eventobject.WriteExtentionsToFileWithoutDot(TxtB_SaveFilePath.Text)
        Else
            eventobject.WriteExtentionsToFile(TxtB_SaveFilePath.Text)
        End If
        MessageBox.Show("Operation Completed Successfully." & vbCrLf & vbCrLf & "Total Directory Scanned: " & eventobject.DirectoryCounter & vbCrLf & "Total Files Scanned: " & eventobject.FilesCounter, "Completion Notification")
        Lbl_FileNames.Text = "Scanning & Writing Completed"
    End If
    Process.Start("notepad.exe", TxtB_SaveFilePath.Text)

End Sub

以下是在线程中调用的函数:

Public Sub RecursivelyScanFiles(ByVal Path As String, ByVal ObjectDb As DialogueBox)

    If Path <> Nothing Then
        For Each File In GetFiles(Path)
            If File.Length < 260 Then
                If System.IO.Path.HasExtension(File) = True Then
                    Extentions.Add(System.IO.Path.GetExtension(File).ToLower)
                End If
                ObjectDb.settext(File) 'Cross-Thread call Through a Delegate
                ObjectDb.Refreshlabel() 'Cross-Thread call Through a Delegate
                RaiseEvent FilesScanned(Me, EventArgs.Empty)
            End If
        Next
    End If
    If Path <> Nothing Then
        For Each SubDir In GetDirectories(Path)
            If SubDir.Length < 248 Then
                Try
                    RaiseEvent DirectoryScanned(Me, EventArgs.Empty)
                    RecursivelyWriteToFile(SubDir, ObjectDb)
                Catch ex As UnauthorizedAccessException
                    Continue For
                Catch ex As Exception
                    MessageBox.Show(ex.ToString)
                End Try
            End If
        Next
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

创建一个全局布尔值(isBusy),并在线程启动时将其设置为True,然后在线程完成时将其设置为False,如下所示:

Private isBusy as Boolean = False

Private Sub Btn_Extract_Click(sender As Object, e As EventArgs) Handles Btn_Extract.Click

    ' Other code goes here

    ThreadFileScanning.Start(eventobject) 'Thread Starts here--<Point A>

    ' This will prevent the thread from continuing 
    ' while the ThreadFileScanning is busy
    While isBusy
        Threading.Thread.Sleep(200)
    End While

    If ChckB_RemoveDot.Checked Then
        eventobject.WriteExtentionsToFileWithoutDot(TxtB_SaveFilePath.Text)
    Else
        eventobject.WriteExtentionsToFile(TxtB_SaveFilePath.Text)
    End If

    ' rest of the code goes here

End Sub

Public Sub RecursivelyScanFiles(ByVal Path As String, ByVal ObjectDb As DialogueBox)
    isBusy = True

    ' Rest of code goes here

    isBusy = False
End Sub

这将阻止第一个帖子在RecursivelyScanFiles处于活动状态时继续。