我的程序中有一个搜索功能,它使用后台工作程序来获得结果。 Progress changed事件用于使用新项更新listview。
Private Sub SearchWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles SearchWorker.ProgressChanged
Dim itmX As ListViewItem
Dim tmpCustomer As CustomerItem
If e.UserState.ToString = "New" Then
lstResults.Items.Clear()
Else
Try
tmpCustomer = e.UserState
itmX = lstResults.Items.Add(tmpCustomer.CustomerName) ' <-- Error here
itmX.Tag = tmpCustomer.CustomerID
itmX.Name = tmpCustomer.CustomerID
itmX.SubItems.Add(tmpCustomer.City)
itmX.SubItems.Add(tmpCustomer.State)
itmX.SubItems.Add(tmpCustomer.Zip)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
progBar.Value = e.ProgressPercentage
Application.DoEvents()
End Sub
我收到此错误
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll
我试过这个,但没有什么区别
Private Sub SearchWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles SearchWorker.ProgressChanged
If e.UserState.ToString = "New" Then
lstResults.Items.Clear()
Else
Try
itmX = lstResults.Items.Add("Test")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
progBar.Value = e.ProgressPercentage
Application.DoEvents()
End Sub
编辑: 哦,如果我只是单步执行代码,它就没有任何问题。
编辑2:
以下是背景工作者DoWork活动:
Sub doSearch(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles SearchWorker.DoWork
canceled = False
If curSearch = doSearchText Then
canceled = True
Exit Sub
End If
curSearch = doSearchText
SearchWorker.ReportProgress(0, "New")
Dim rSelect As New ADODB.Recordset
Dim CustomerID As Integer = MakeNumeric(doSearchText)
Dim sSql As String = "SELECT DISTINCT CustomerID, CustomerName, City, State, Zip FROM qrySearchFieldsQuick WHERE "
Dim sWhere As String = "CustomerID = " & CustomerID & " OR CustomerName Like '" & doSearchText & "%'"
If Not doSearchText.Contains(" ") Then
sWhere &= " OR FirstName Like '" & doSearchText & "%' OR LastName Like '" & doSearchText & "%'"
Else
Dim str() As String = doSearchText.Split(" ")
sWhere &= " OR (FirstName Like '" & str(0) & "%' AND LastName Like '" & str(1) & "%')"
End If
Dim i As Integer = 0
Dim tmpCustomer As CustomerItem
With rSelect
.Open(sSql & sWhere & " ORDER BY CustomerName", MyCn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
Do While Not .EOF
If SearchWorker.CancellationPending Then
canceled = True
Exit Do
End If
Do While IsDBNull(.Fields("CustomerID").Value)
.MoveNext()
Loop
tmpCustomer.CustomerID = "c" & .Fields("CustomerID").Value
tmpCustomer.CustomerName = NZ(.Fields("CustomerName").Value, "").ToString.Trim
tmpCustomer.City = Trim(NZ(.Fields("City").Value, ""))
tmpCustomer.State = Replace(Trim(NZ(.Fields("State").Value, "")), ",", "")
tmpCustomer.Zip = Trim(NZ(.Fields("Zip").Value, ""))
SearchWorker.ReportProgress((i / .RecordCount) * 100, tmpCustomer)
i += 1
Application.DoEvents()
aMoveNext:
.MoveNext()
Loop
.Close()
End With
End Sub
答案 0 :(得分:6)
我认为问题可能就在这一行:
Application.DoEvents()
如果您的BackgroundWorker
排队ProgressChanged
个事件的速度足够快,则对Application.DoEvents()
的每次调用都将通过消息队列进行,来ProgressChanged
事件,更新进度,调用Application.DoEvents()
,处理消息队列,进入ProgressChanged
事件等。在代码中导致递归行为。
尝试删除该呼叫并查看问题是否消失。
答案 1 :(得分:5)
Application.DoEvents()
那是麻烦制造者。您添加它是因为您注意到即使您使用了BGW,用户界面仍然会冻结。问题是,当它抽出事件时,您的BGW再次调用了ReportProgress。导致ProgressChanged再次运行。导致DoEvents再次被调用。这可能会持续几秒钟,直到ui线程耗尽堆栈空间。 KABOOM。
您必须删除DoEvents()调用。并且解决了真正的问题,你的BGW经常调用ReportProgress 方式。导致ui线程被调用请求调用ProgressChanged。导致它不再负责其常规职责。包括绘画和回应用户输入。
每秒调用ReportProgress的频率不超过20次。人眼看起来很光滑。收集计算结果,这样您就可以准备好处理一大堆工作了。如果你的工作者产生的结果比ui线程可以显示的速度快,那么你别无选择,只能强行减速。