Cancelling Background Tasks

后端 未结 5 1447
南笙
南笙 2021-02-05 21:11

When my C# application closes it sometimes gets caught in the cleanup routine. Specifically, a background worker is not closing. This is basically how I am attempting to close

5条回答
  •  日久生厌
    2021-02-05 21:39

    This code is guaranteed to deadlock when the BGW is still running. BGW cannot complete until its RunWorkerCompleted event finished running. RunWorkerCompleted cannot run until the UI thread goes idle and runs the message loop. But the UI thread isn't idle, it is stuck in the while loop.

    If you want the BGW thread to complete cleanly, you have to keep your form alive. Check this thread to see how to do that.

提交回复
热议问题