這是我的應用程序,您可以看到3個按鈕,啓動按鈕啓用,暫停禁用並停止禁用。窗體不要等待線程完成並掛起我的應用程序
的問題是我有一個獨立的線程在我的形式做一個「過程」(在黑RichTextBox的打印信息),我的意圖是可以暫停或停止它,但是當我啓動線程時,暫停按鈕和停止按鈕將在一秒鐘內變爲禁用狀態。
我可以告訴的形式與_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
回答使用將CheckForIllegalCrossThreadCalls設置爲False的代碼的問題毫無意義。刪除並修復你得到的異常。 –
如果你只是提供一個簡單的例子,比如一個包含儘可能少的控件並儘可能少的代碼來重現問題的表單會更好。 –
我不知道爲什麼有些人投票結束我的問題...我一直試圖過多的日子,只是爲了得到那個線程的作品,現在我只想學習和完成這個......真的在哪裏你發現比我的更多信息的問題?圖像和解釋和代碼示例... ofcourse我的幽靈朋友,如果你能做得比我好,然後試圖幫助我,而不是結束我的問題。非常感謝您的投票。 – ElektroStudios