为什么线程仍然是AbortRequested? [英] Why does thread remain AbortRequested?

查看:331
本文介绍了为什么线程仍然是AbortRequested?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有第二个线程,我从父线程调用t.abort()。我

将t状态视为AbortRequested。它保持这种方式已有一段时间了。在

条件下,线程会保持这种状态吗?


t的子程序调用是一个内部代码的inifite循环。在每次交互结束时,这个sub()将调用currentthread.sleep(x)。

这样可以正常工作。我想父线程不能中止子线程,

t,直到无限循环完成,它永远不会。


如果我从父线程请求t.suspend()或t.sleep()然后

t.abort(),这是否允许线程中止或我是否需要中断

线程?


谢谢,

Brett

解决方案

< BLOCKQUOTE>"布雷特" < no@spam.com> schrieb:

我有第二个线程,我从父线程调用t.abort()。我将t状态视为AbortRequested。它保持这种方式已有一段时间了。在
条件下,线程会保持这种状态吗?


如果你需要阻止

当前线程,那么在调用''t.Abort'后立即调用't.Join()''中止线程死亡。

t的子程序调用是一个内部代码的inifite循环。




在这种情况下,使用由线程检查的布尔变量(对于

示例,每次循环的第n次迭代)是IMO的首选方式。更多

信息:


如何在.NET中停止线程(以及为什么''Thread.Abort''是邪恶的)

< URL:http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation>


正常关闭工作线程

< URL:http://www.yoda.arachsys.com/csharp/multithreading.html#worker.threads>


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http:// dotnet.mvps.org/dotnet/faqs/>




" Herfried K. Wagner [MVP]" <喜*************** @ gmx.at>在消息中写道

news:e%**************** @ tk2msftngp13.phx.gbl ...

"布雷特" < no@spam.com> schrieb:

我有第二个线程,我从父线程调用t.abort()。我将t状态视为AbortRequested。它保持这种方式已有一段时间了。在
条件下线程会保持这种状态吗?



如果你需要调用''t.Abort'',请立即调用't.Join()''阻止当前线程直到中止的线程死亡。

t的子过程。调用是一个内部代码的inifite循环。



在这种情况下,使用由线程检查的布尔变量(例如,每个第n次迭代loop)是IMO的首选方式。更多信息:

如何在.NET中停止线程(以及为什么'Thread.Abort''是邪恶的)
< URL:http:// www。 interact-sw.co.uk/iangblog/2004/11/12/cancellation>

优雅地关闭工作线程
< URL:http://www.yoda.arachsys.com /csharp/multithreading.html#worker.threads>

- MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/> ;
VB< URL:http://dotnet.mvps.org/dotnet/faqs/>




感谢您的链接,我会尝试你提到的布尔方法。


当我调试一个有两个线程的应用程序时,我可以看到它有时会从类文件中切换回b $ b每隔一行的形式,取决于

我在哪里。是真的多线程应用程序实际上并没有在
并行运行线程吗?他们只是轮流执行一小段执行时间。


并行处理器执行指令。难道这不允许两个

线程并行运行而不是分享时间吗?


谢谢,

Brett


Brett,

" Brett" < no@spam.net> schrieb:

当我调试一个有两个线程的应用程序时,我可以看到它有时会从类文件切换回每隔一行的表单,具体取决于
我在哪里。是真的多线程应用程序实际上并不是并行运行线程吗?他们只是轮流执行一小段执行时间。


这是多线程应用程序的典型特征。

某些处理器并行执行指令。难道这不允许两个线程并行运行而不是共享时间吗?




在单处理器机器上,多个线程运行伪并行,

表示每个线程都有其时间片。如果你在一个

多线程系统上运行应用程序,线程可以由不同的CPU执行。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http://dotnet.mvps.org / dotnet / faqs />


I have a second thread in which I call t.abort() from the parent thread. I
see "t"s state as AbortRequested. It stays this way for some time. Under
which conditions will a thread remain this way?

The sub procedure that "t" calls is an inifite loop with code inside. Near
the end of each interation, this sub() will call currentthread.sleep(x).
That works fine. I imagine the parent thread can''t abort the child thread,
"t", until the inifinite loop finishes, which it never will.

If I request a t.suspend() or t.sleep() from the parent thread then
t.abort(), does that allow the thread to abort or do I need to interrupt the
thread?

Thanks,
Brett

解决方案

"Brett" <no@spam.com> schrieb:

I have a second thread in which I call t.abort() from the parent thread. I
see "t"s state as AbortRequested. It stays this way for some time. Under
which conditions will a thread remain this way?
Call ''t.Join()'' immediately after calling ''t.Abort'' if you need to block the
current thread until the aborted thread died.
The sub procedure that "t" calls is an inifite loop with code inside.



In this case, using a boolean variable that is checked by the thread (for
example, every n-th iteration of the loop) is IMO the preferred way. More
information:

How To Stop a Thread in .NET (and Why ''Thread.Abort'' is Evil)
<URL:http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation>

Shutting down worker threads gracefully
<URL:http://www.yoda.arachsys.com/csharp/multithreading.html#worker.threads>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e%****************@tk2msftngp13.phx.gbl...

"Brett" <no@spam.com> schrieb:

I have a second thread in which I call t.abort() from the parent thread. I
see "t"s state as AbortRequested. It stays this way for some time. Under
which conditions will a thread remain this way?



Call ''t.Join()'' immediately after calling ''t.Abort'' if you need to block
the current thread until the aborted thread died.

The sub procedure that "t" calls is an inifite loop with code inside.



In this case, using a boolean variable that is checked by the thread (for
example, every n-th iteration of the loop) is IMO the preferred way. More
information:

How To Stop a Thread in .NET (and Why ''Thread.Abort'' is Evil)
<URL:http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation>

Shutting down worker threads gracefully
<URL:http://www.yoda.arachsys.com/csharp/multithreading.html#worker.threads>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



Thanks for the links and I will try the boolean method you mention.

When I''m debugging an app with two threads, I can see it sometimes will
switch from the class file back to the form every other line, depending on
where I am. Is it true multi threaded apps don''t actually run threads in
parallel? They just take turns with small slices of execution time.

Some processors execution instructions in parallel. Wouldn''t this allow two
threads to run in parallel rather than share time?

Thanks,
Brett


Brett,

"Brett" <no@spam.net> schrieb:

When I''m debugging an app with two threads, I can see it sometimes will
switch from the class file back to the form every other line, depending on
where I am. Is it true multi threaded apps don''t actually run threads in
parallel? They just take turns with small slices of execution time.
That''s typical for multithreaded applications.
Some processors execution instructions in parallel. Wouldn''t this allow
two threads to run in parallel rather than share time?



On a single-processor machine, multiple threads run pseudo-parallel, which
means that each thread has its timeslice. If you run the application on a
multithreading system, threads can be executed by different CPUs.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


这篇关于为什么线程仍然是AbortRequested?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆