Is it possible to kill the BackgroundWorker's thread?

后端 未结 2 2059
小鲜肉
小鲜肉 2021-01-18 09:20

Is it possible \"kill\" the thread of a BackgroundWorker?

In my DoWork event, I can\'t check the cancellation flag, because I have a blocki

2条回答
  •  孤独总比滥情好
    2021-01-18 09:49

    I'm not aware of a safe way to abort a thread.

    A background worker can check if it should cancel and cancel itself, but if it's off querying a database there's not much you can do until it returns.

    You can do work on a ThreadPool thread and just abandon the thread and start another if it's not needed anymore (making sure that when it comes back from a db query it checks if it should cancel before doing anything nasty). Of course you'd need to balance the performance and manage thread synchronisation. If you go down that path, you could take a look at the Interlocked static class for efficient critical section locking.

提交回复
热议问题