Cancelling Background Tasks

后端 未结 5 1449
南笙
南笙 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:33

    In the background worker thread you need to check the BackgroundWorker.CancellationPending flag and exit if it is true.

    The CancelAsync() just sets this flag.

    Or to put it another way. CancelAsync() doesn't actually cancel anything. It won't abort the thread or cause it to exit. If the worker thread is in a loop and checks the CancellationPending flag periodically it can catch the cancel request and exit.

    MSDN has an example here although it doesn't use a loop in the worker routine.

提交回复
热议问题