C# 线程.Abort()
如果一个线程正在运行一个函数func1
,该函数在其内部调用另一个函数func2
...
然后我调用了thread.Abort()
这会停止<仅代码>func1
OR func1
和 func2
以及 func1
调用的所有函数?
谢谢
编辑:这里有更多详细信息:
func1
在新线程中调用,它定期连续调用 func2
...
仅当某个数组不为空时,func2
才开始执行某些工作。它完成并返回。
当主管想要保存数据时,它会中止 func1
的线程 - 然后创建数组null,保存数据,然后用新的数组填充数组..并再次使用func1
启动线程..
有时会引发异常,因为func2
中的数组为空..所以func1 中止不影响 func2
If a thread is running a function func1
that calls another function func2
inside it...
Then I called thread.Abort()
Will this stop func1
only
OR func1
and func2
and all the functions func1
has called??
Thanks
Edit: Here are more detail:
func1
is called in a new thread, it continuously calls func2
on regular basis...func2
begin doing some work only if some array is not null.. it finishes it and return
When supervisor wants to save data, it aborts Thread of func1
- and then makes array null, saves data, then fill in the array with new one.. and starts Thread with func1
again..
Sometimes exception is raised because array is null in func2
.. so func1 abort did not affect func2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Thread.Abort
不保证停止线程,如果可能的话应该避免使用它。强调我的。
它的作用是引发
ThreadAbortException
< /a> 在目标线程中。如果捕获此异常,代码将继续执行,直到到达 catch 块的末尾,此时异常将自动重新引发。如果您没有捕获它,它与普通异常类似 - 它会在调用堆栈中向上传播。假设您没有捕获异常,则该线程中运行的所有代码都将停止运行。从该线程启动的其他线程不会受到影响。
Thread.Abort
is not guaranteed to stop the thread and you should avoid using it if possible.Emphasis mine.
What it does is raise a
ThreadAbortException
in the target thread. If you catch this exception, the code will continue executing until it reaches the end of the catch block, at which point the exception is automatically rethrown. If you don't catch it, it is similar to a normal exception - it propagates up the call stack.Assuming you don't catch the exception, all the code running in that thread will stop running. Other threads that were started from that thread will not be affected.
该线程内启动的任何操作都将被中止。
Anything started within that thread will be aborted.
您可能会面临竞争条件,即主例程在 ThreadAbortException 到达 func1 线程之前但在 func2 检查空数组之后将数组清空。
至少,您的主代码和 func2 应在数组周围使用锁。您还应该在再次重新启动之前测试 func1 线程是否已死亡。正如其他人所说,用信号通知线程停止,而不是依赖 Thread.Abort。
根据您的描述,我不能 100% 确定 func2 是从 func1 线程内调用的,但如果 func2 在从 func1 内启动的不同线程上运行,则终止 func1 线程不会影响 func2,因为所有线程都作为子线程存在您的父进程的,而不是它们启动的线程的。
You could be facing a race condition, where your main routine nulls the array before the ThreadAbortException reaches the func1 thread but after func2 checks for null array.
As a minimum, your main code and func2 should use a lock around the array. You should also test that the func1 thread is dead before restarting it again. And as everyone else has said, signal a thread to stop rather than relying on Thread.Abort.
I'm not 100% sure from your description that func2 is called from within the func1 thread, but if func2 is run on a different thread that's started from within func1, killing the func1 thread won't affect func2 as all threads exist as children of your parent process, not of the thread that they were started from.
对于谁在乎:
经过更多调试,我发现线程在启动之前再次初始化;导致线程在后台运行...
后台未引用的线程不受 Abort() 影响...因此它将继续工作并尝试使用空数组...
最后:
Abort() 将结束您的线程,除非在某些情况下(上面在其他人的答案中提到)
Abort() 在未被引用后不会结束线程(显然)
谢谢!!
For who cares:
After more debugging I found that the thread is initialized once more before start; causing a thread to run in background...
This unreferenced one in background is not affected with Abort()... so it will continue working and it tries to use null array...
At the end:
Abort() will end your thread, except in some conditions (mentioned above in other's answer)
Abort() will not end a thread after being unreferenced (obviously)
Thanks!!