C# 线程.Abort()

发布于 2024-09-02 08:48:43 字数 560 浏览 5 评论 0原文

如果一个线程正在运行一个函数func1,该函数在其内部调用另一个函数func2...

然后我调用了thread.Abort()

这会停止<仅代码>func1
OR func1func2 以及 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦巷 2024-09-09 08:48:43

Thread.Abort 不保证停止线程,如果可能的话应该避免使用它。

调用此方法通常会终止线程。

强调我的。

它的作用是引发 ThreadAbortException< /a> 在目标线程中。如果捕获此异常,代码将继续执行,直到到达 catch 块的末尾,此时异常将自动重新引发。如果您没有捕获它,它与普通异常类似 - 它会在调用堆栈中向上传播。

假设您没有捕获异常,则该线程中运行的所有代码都将停止运行。从该线程启动的其他线程不会受到影响。

Thread.Abort is not guaranteed to stop the thread and you should avoid using it if possible.

Calling this method usually terminates the thread.

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.

终止放荡 2024-09-09 08:48:43

该线程内启动的任何操作都将被中止。

Anything started within that thread will be aborted.

木落 2024-09-09 08:48:43

您可能会面临竞争条件,即主例程在 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.

多情癖 2024-09-09 08:48:43

对于谁在乎:
经过更多调试,我发现线程在启动之前再次初始化;导致线程在后台运行...

    Thread T
    T=new Thread(func1);
    // Some code...
    // Start:
    T=new Thread(func1);

后台未引用的线程不受 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...

    Thread T
    T=new Thread(func1);
    // Some code...
    // Start:
    T=new Thread(func1);

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!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文