What is a safe way to stop a running thread?

后端 未结 2 630
滥情空心
滥情空心 2020-12-01 19:11

I have a thread which contains execution of an IronPython script. For some reason I may need to stop this thread at any time, including script execution. How to achieve this

2条回答
  •  悲哀的现实
    2020-12-01 19:54

    What is a safe way to stop a running thread?

    Put the thread in its own process. When you want it to stop, kill the process.

    That is the only safe way to kill a thread. Aborting a thread can severely destabilize a process and lose user data. There's no way to avoid the "lose user data" scenario if you really, truly need to be able to kill a thread that could be doing anything. The only way to avoid destabilizing the process that is calling for the abort is to make them different processes entirely.

提交回复
热议问题