BackgroundWorker exception handling

前端 未结 1 584
太阳男子
太阳男子 2021-02-20 15:01

I\'m working with the following components:

  • a Library (which throws an exception)
  • a test-console to test my logging
  • the enterprise library except
相关标签:
1条回答
  • 2021-02-20 15:16

    That is the correct pattern for BackgroundWorker.

    I suspect the problem is that your Main method is exiting before the BW has completed.

    RunWorkerAsync will return immediately and if you are not waiting in Main, then your process will end, perhaps even before the BW has started, never mind completed.

    Try adding a Console.ReadLine at the end of your Main method.


    Out of interest:

    BW behaves differently in a Console app and a Windows app. If you use a WinForms or WPF app, there will be a derived SynchronizationContext on your UI thread and BW will marshal RunWorkerCompleted back to the UI thread and run it there. That's one of the main benefits of BW.

    In a Console App, the default SynchronizationContext is used and this marshals RunWorkerCompleted onto a thread pool thread. This means you can block the Main thread and the completed handler will still run.

    0 讨论(0)
提交回复
热议问题