-1

这是我的应用程序,您可以看到 3 个按钮,启用开始按钮、禁用暂停和禁用停止。

在此处输入图像描述

问题是我的表单中有一个单独的线程来创建一个“进程”(并在黑色富文本框中打印信息),我的意图是可以暂停或停止它,但是当我启动线程时,暂停按钮和停止按钮在一秒钟内将启用变为禁用。

我可以告诉表单在使用_WaitHandle_FirstThreadDone.WaitOne()启动线程后等待,然后我可以看到启用了暂停和停止按钮,但问题是我的应用程序挂起,直到“进程”完成..所以我不能按任何按钮。

拜托,我需要帮助才能做到这一点...

我的表格的重要部分:

Public Class Form1

#Region "Append text function"

    ' Append Text
    Public Sub AppendText(box As RichTextBox, color As Color, text As String)

        Control.CheckForIllegalCrossThreadCalls = False

        Dim start As Integer = box.TextLength
        box.AppendText(text)
        Dim [end] As Integer = box.TextLength

        ' Textbox may transform chars, so (end-start) != text.Length
        box.[Select](start, [end] - start)
        If True Then
            box.SelectionColor = color
            ' could set box.SelectionBackColor, box.SelectionFont too.
        End If
        box.SelectionLength = 0
        ' clear
    End Sub

#End Region


#Region "Thread"

    Public _WaitHandle_FirstThreadDone As New System.Threading.AutoResetEvent(False)

    Public Sub ThreadProc(ByVal aDir As DirectoryInfo)

        Dim aFile As FileInfo

        For Each aFile In aDir.GetFiles()

            If accepted_extensions.ToLower.Contains(aFile.Extension.ToLower) Then

                ' print output
                AppendText(consolebox, Color.Yellow, "Processing: ")
                AppendText(consolebox, Color.White, aFile.ToString() + vbNewLine)
                consolebox.ScrollToCaret()
                processedfiles += 1
                totalfiles_label.Text = "Processed " + processedfiles.ToString() + " of " + totalfiles.ToString() + " total video files"

                ' MEDIAINFO:  (ac3, dts, wav and multitrack)
                If ac3 = True Or dts = True Or wav = True Or multitrack = True Then

                    MI.Open(aFile.FullName)

                    Dim Pos As Integer = 0
                    To_Display = Nothing

                    While Pos < MI.Count_Get(StreamKind.Audio)

                        ' AC-3
                        If ac3 = True Then
                            If MI.Get_(StreamKind.Audio, Pos, "Format").ToString() = "AC-3" Then
                                results_box.AppendText("AC3 Track: " + aFile.FullName.ToString() + vbNewLine)
                                results_box.SelectionStart = results_box.Text.Length
                                results_box.ScrollToCaret()
                                problems += 1
                                problems_label.Text = problems.ToString() + " problems found"
                            End If
                        End If

                        System.Math.Max(System.Threading.Interlocked.Increment(Pos), Pos - 1)
                    End While
                End If
            End If
        Next

        _WaitHandle_FirstThreadDone.Set()
    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
        Dim t As New Threading.Thread(AddressOf ThreadProc)
        t.Start(aDir)
        '
        For Each nextDir In aDir.GetDirectories
            If playlist = True Then
                Using writer As StreamWriter = New StreamWriter(aDir.FullName & "\" & nextDir.Name & "\" & nextDir.Name & ".m3u", False, System.Text.Encoding.UTF8)
                    'overwrite existing playlist
                End Using
            End If
            MediaInfoWorkWithDirectory(nextDir)
        Next
    End Sub

#End Region




#Region "Action buttons"

    ' start button
    Public Sub Button2_Click(sender As Object, e As EventArgs) Handles start_button.Click


                consolebox.Clear()

                ' pause / cancel button ON
                start_button.Enabled = False
                pause_button.Enabled = True
                cancel_button.Enabled = True

                ' Organization process
                NameOfDirectory = userSelectedFolderPath
                MediaInfo(NameOfDirectory)
                '  _WaitHandle_FirstThreadDone.WaitOne()
                consolebox.AppendText(vbNewLine + "[+] Organization finalized!" + vbNewLine)
                consolebox.Refresh()
                consolebox.SelectionStart = consolebox.Text.Length
                consolebox.ScrollToCaret()

                ' pause / cancel button OFF
                start_button.Enabled = True
                pause_button.Enabled = False
                cancel_button.Enabled = False

    End Sub

#End Region


    Private Sub pause_button_Click(sender As Object, e As EventArgs) Handles pause_button.Click
        paused = True
    End Sub
End Class
4

1 回答 1

2

应用程序挂起的原因是程序正在按顺序爆破数据。您应该在循环的部分内添加一个 if 语句,以检查处理之间的暂停条件。像您一样将开/关控件放在子例程中并不是一个好主意,因为它只能在所有操作完成后启用按钮。

IE停止进程

对于 i 到 10 做
如果 checkbox1.checked = True then Exit Sub '检查停止条件
'处理视频
环形

要暂停它,您可以实现停止,但让它记住在恢复时从哪里开始。

还有,为什么你有死亡的面孔?那东西会杀死脑细胞。

于 2012-11-30T05:49:03.567 回答