如何轻轻终止线程??? [英] How do I gently terminate a thread???

查看:64
本文介绍了如何轻轻终止线程???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

来自win32 API的
我回想起一个ExitThread()调用,从同一个线程内轻轻终止一个

线程....但现在我只能看到一个Abort

调用,这对我来说似乎是TerminateThread()Win32 API的一个包装器,这是一个结束线程的残酷方式。有没有其他更温和的方式来关闭

线程???


Bob Rock

解决方案

Bob,


你^可以^调用ExitThread API退出线程。但是,

管理的Thread概念和实际实现可能并不总是同步(也就是说,如果这个线程来自线程池,那么结果是

也是不可预测的)。例如,将来可能会使用光纤来支持线程的托管概念。


如果有的话,我会使用返回一个方法或其他

指标的值来退出一个线程,而不是使用ExitThread。对于bash而言,这似乎是一个糟糕的实施方案。这样的话。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP] < br $> b $ b - mv*@spam.guard.caspershouse.com


Bob Rock <无*************************** @ hotmail.com>在消息中写道

news:ex ************** @ TK2MSFTNGP12.phx.gbl ...

你好,
来自win32 API我记得一个ExitThread()调用,从同一个线程内部轻轻终止一个
线程....但现在我只能看到一个中止
调用似乎给我一个TerminateThread()Win32 API的包装器,
是一种结束线程的残酷方式。有没有其他更温和的方式来关闭
a线程???

Bob Rock



Bob Rock写道:

来自win32 API我回想起一个ExitThread()调用,轻轻地从同一个线程中终止一个线程....


你永远不应该直接调用ExitThread,因为这通常会导致内存泄漏(至少来自CRT)。

但现在我只能
看到是一个Abort调用,在我看来是一个封装器上的终结线程(Win32),这是一种结束线程的野蛮方式。是否还有其他更温和的方式关闭线程???




返回!?


- -

问候

Jochen


你需要一个内存泄漏查找器吗?
http://www.codeproject.com/tools/leakfinder.asp


您是否需要服务器的每日报告?
http ://sourceforge.net/projects/srvreport/


比如说我有一个线程,我发起了'正在运行的一些循环

代码'监控状态或某事。我知道我可以在共享地址空间中定义一个

变量,并且直到变量

变为false或者某个东西,然后让线程退出。有没有使用这种方法导致线程关闭而没有

调用Abort()?

Ryan Gregg

" Jochen Kalmbach" <无******************** @ holzma.de>在消息中写道

news:Xn ********************************* @ 127.0。 0.1 ...

Bob Rock写道:

来自win32 API我回想起一个ExitThread()调用来轻轻地从内部终止一个线程相同的线程......



你永远不应该直接调用ExitThread,因为这会导致内存泄漏(至少来自CRT)。
< blockquote class =post_quotes>但是现在我所能看到的是一个Abort调用,在我看来这是一个封装器,它是一个结束线程的野蛮方式的终结线程Win32 API。是否还有其他更温和的方法来关闭一个帖子???



返回!?

-
问候

你需要一个内存泄漏查找器吗?
http://www.codeproject.com/tools/leakfinder.asp

您是否需要来自服务器的每日报告?
http://sourceforge.net/projects/srvreport/



Hello,

coming from win32 API I recall an ExitThread() call to gently terminate a
thread from inside the same thread .... but now all I can see is an Abort
call which seems to me a wrapper on the TerminateThread() Win32 API which is
a brutal way to end a thread. Is there any other more gentle way to close a
thread???

Bob Rock

解决方案

Bob,

You ^could^ call the ExitThread API to exit the thread. However, the
managed concept of a Thread and the actual implementation may not always be
in synch (also, if this thread is from the thread pool, then the results are
unpredictable as well). It is possible that in the future that fibers will
be used to support the managed concept of a thread, for example.

If anything, I would use return values from a method or some other
indicator to exit a thread, not using ExitThread. It just seems like a bad
implementation to "bash" the thread like that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...

Hello,

coming from win32 API I recall an ExitThread() call to gently terminate a
thread from inside the same thread .... but now all I can see is an Abort
call which seems to me a wrapper on the TerminateThread() Win32 API which is a brutal way to end a thread. Is there any other more gentle way to close a thread???

Bob Rock



Bob Rock wrote:

coming from win32 API I recall an ExitThread() call to gently
terminate a thread from inside the same thread ....
You never should call ExitThread directly, because this normaly leads to
memory leaks (at least from the CRT).
but now all I can
see is an Abort call which seems to me a wrapper on the
TerminateThread() Win32 API which is a brutal way to end a thread. Is
there any other more gentle way to close a thread???



return !?

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/


Say for instance I have a thread that I fire off that''s running some loop of
code that''s monitoring status or something. I know I could define a
variable in a shared address space, and have that loop until the variable
goes false or something, which would then have the thread exit. Is there a
way though without using this method to cause a thread to shut down without
calling Abort() ?
Ryan Gregg
"Jochen Kalmbach" <no********************@holzma.de> wrote in message
news:Xn*********************************@127.0.0.1 ...

Bob Rock wrote:

coming from win32 API I recall an ExitThread() call to gently
terminate a thread from inside the same thread ....



You never should call ExitThread directly, because this normaly leads to
memory leaks (at least from the CRT).

but now all I can
see is an Abort call which seems to me a wrapper on the
TerminateThread() Win32 API which is a brutal way to end a thread. Is
there any other more gentle way to close a thread???



return !?

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/



这篇关于如何轻轻终止线程???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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