【问题标题】:DoWork of backgroundworker skipped on another computer在另一台计算机上跳过后台工作人员的 DoWork
【发布时间】: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 线程。不确定这是否有帮助。祝你好运..

标签: .net vb.net


【解决方案1】:

可能您的 extract_cycle 方法引用了目标机器上不存在的程序集;因此在 JITted 时会抛出异常(因此在执行 MsgBox("test2") 之前)。

如果您catch unhandled exceptions(甚至查看事件日志),您应该能够看到正在发生的事情。

【讨论】:

  • 谢谢!使用 Try Catch ex As Exception 我发现它是“无法加载文件或程序集 'Ionic.Zip'”,它是一个 .dll,我该如何包含它?
  • @Kaos,您可以将其复制到与其他程序集相同的目录,或者可能运行将其安装在 GAC 中的安装程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多