【发布时间】:2012-11-24 22:32:06
【问题描述】:
在一个项目中,我正在使用 Backgroundworker(在 vb.net 中)。 我在 Windows 8 Pro(64 位)下工作,项目在上面运行良好......但是如果我在 Windows 7(64 位)的机器上传输可执行文件,就会发生奇怪的事情! Backgroundworker 的 DoWork 不起作用,它直接跳到 RunWorkerCompleted。 我不明白为什么。 我用 Visual Studio 2012 Express Edition 编译它。 你有解决这个问题的想法吗? 谢谢。
这是一些代码:
Private Sub bgw_extract_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw_extract.DoWork
MsgBox("test dowork") 'this appear
extract_cycle() 'this isn't executed
MsgBox("end dowork") 'this isn't executed
End Sub
Private Sub extract_cycle()
'nothing is executed here
MsgBox("test2") 'not executed
Try
'my code....
Catch ex As Exception
MessageBox.Show(ex.ToString(), "Extract Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub bgw_extract_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
bgw_extract.RunWorkerCompleted
MsgBox("RunWorkCompleted") 'this is executed in every case
End Sub
【问题讨论】:
-
您是否尝试过用一个最小的例子来缩小问题范围?
-
你能显示一些代码吗,很难猜到你在做什么?
-
用一个最小的例子工作(如果我把三个 MsgBox 只放在 DoWork 它.. 工作)。只有使用 extract_cycle(),DoWork 只运行它包含的第一行,然后跳到 RunWorkerCompleted。这一切都只在我的 Windows 8 电脑上执行。
-
我通常做的是,避免在
DoWork()事件中使用 UI 相关的操作。例如,调用MessageBox.Show()或更新控件。所以,我也不会调用任何引用它的函数。我只更新ProgressChanged()事件和RunWorkerCompleted()事件中的UI 线程。不确定这是否有帮助。祝你好运..