【发布时间】:2016-12-08 09:01:46
【问题描述】:
我使用 Visual Studio 2013 for Visual Basic,但我正在努力调试我的多线程程序。
我正在使用 BackgroundWorker,它的工作方式似乎与我认为的不同。
我不明白为什么我的程序在只处理了我的 ArrayList 中名为 arFileName 的第一个条目后就停止了。
BackgroundWorker1.DoWork 过程中的For Each 语句无法遍历以下代码中的整个arFileName:
Private Sub btnRun_Click(sender As Object, e As EventArgs) Handles btnSelectCsv.Click
'arFileName is ArrayList and it has enormous counts
ProgressBar1.Maximum = arFileName.Count
Me.Cursor = Cursors.WaitCursor
'Do background
BackgroundWorker1.RunWorkerAsync()
Me.Cursor = Cursors.Arrow
MessageBox.Show("Finished!", "info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) _
Handles BackgroundWorker1.DoWork
Dim arNotFoundFile As New ArrayList
'Confirm file exists
For Each filename As String In arFileName ' Here!
If Not IO.File.Exists(filename) Then
arNotFoundFile.Add(filename)
ProgressBar1.Value = ProgressBar1.Value + 1
End If
Next
End Sub
【问题讨论】:
-
ProgressBar 不会更新,因为它不在 GUI 线程上运行。您必须启用报告进度并从那里处理事件
标签: vb.net visual-studio-2013 backgroundworker postmortem-debugging