在程序退出时终止所有线程 [英] terminating all threads on program exit

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

问题描述

当主窗体关闭时,线程不会自动终止(不是它们应该是
)。当

程序退出时,终止它们的最佳方法是什么?抓住表单的关闭消息,并向必须关闭的线程发出

信号?似乎我需要一个

析构函数。表单没有可覆盖的Dispose方法。

那么,当表单关闭时我该如何反应?


注意我的示例线程(异常)永远运行,直到它被告知

通过布尔数据成员关闭。因此,表格的'close方法

可以设置这个。 (我知道有Abort()方法,但是

看起来很难看,特别是因为它不能保证工作。)


Zytan

Threads are not auto-terminated when the main form closes (not that
they should be). What''s the best way to terminate them when the
program exits? Catch the on close message of the form, and give a
signal to the threads that they must shut down? Seems I need a
destructor for that. Forms don''t have an overridable Dispose method.
So, how can I react when the form closes?

Note my example thread just (abnormally) runs forever until it is told
to shut down via boolean data member. So, the form''s close method
could just set this. (I know there''s the Abort() method, but that
seems ugly, especially since it''s not guaranteed to work.)

Zytan

推荐答案

表格没有可覆盖的Dispose方法。
Forms don''t have an overridable Dispose method.

那么,当表单关闭时我该如何反应?
So, how can I react when the form closes?



FormClosed,FormClosing。


我不知道哪个更好?我怀疑在这种情况下是否重要。


Zytan

FormClosed, FormClosing.

I wonder which is better? I doubt it matters in this case.

Zytan


表单没有可覆盖的Dispose方法。
Forms don''t have an overridable Dispose method.

那么,当表格关闭时我该如何反应?
So, how can I react when the form closes?



FormClosed,FormClosing。


我不知道哪个更好?在这种情况下,我怀疑这很重要。


FormClosed, FormClosing.

I wonder which is better? I doubt it matters in this case.

http://msdn2.microsoft.com/en-us/lib...rmclosing.aspx

当表单关闭时,它会被释放,释放所有资源

与表单相关联。如果您取消此活动,表格仍然打开




我想如果您处理FormClose,我可以取消关闭。

但是,如果你在FormClosed中,它已经处理好了所有东西,意思是

它可能为时已晚,无法通知线程关闭。但是,那么使用它的是什么?b $ b?如果已经处理了

表单,你可以在FormClosed中做什么?类数据成员必须仍然存在,

因此意味着清理东西还为时不晚,例如关闭

线程。


Zytan

http://msdn2.microsoft.com/en-us/lib...rmclosing.aspx
"When a form is closed, it is disposed, releasing all resources
associated with the form. If you cancel this event, the form remains
opened."

I thought maybe you can cancel the close if you handle FormClosing.
But, if you are in FormClosed, everything it already disposed, meaning
it''s probably too late to signal the threads to close. But, what''s
the use of it, then? What could you possibly do in FormClosed if the
form is already disposed? The class data members must still exist,
thus meaning it''s not too late to clean up stuff, like closing
threads.

Zytan


HI,

我的情况相同。

我的当前的解决方案是,

我将线程独立封装在一个类中,并且我有一个Stop()公共

方法来终止所有线程

//这样的线程proc,其中m_alive是私有数据库

private void ThreadProc()

{

while( m_alive)

{...}

}


//

public void Stop( )

{

m_alive = false;

Thread.Sleep(1000);

}


并在form1.Closing事件中引用类的形式,

if(threadClass.Alive)threadClass.Stop();


另外,我想这个解决方案通过似乎运作良好。


---------------------- ------ -------------------------------------------------- ----------

在我以前的项目中,我使用WinApi和Delphi,最好的解决方案就像

这个,


//线程的过程

函数ThreadProc(const p:Pointer):Cardinal; stdcall;

begin

当WaitMessage做的同时PeekMessage(m,0,0,0,PM_REMOVE)开始时

case m.message

WM_QUIT:退出;

WM_OTHER_MSG:....

结束

结束;


//当我想终止这个帖子时,只发帖一条消息到线程

PostThreadMessage(FThrID,WM_QUIT,0,0);

//其中FThrId是线程的ID


如何和我们在c#中做什么?

如何和我们将一个消息发布到一个线程,以便threadproc可以做某个

的事情?


等待专家的回复。

I have a same situation.
And my current solution is,
I encapsulate the thread in a class independently,and I have a Stop() public
method to terminal all the thread

//thread proc like this,where m_alive is a private datamember
private void ThreadProc()
{
while(m_alive)
{...}
}

//
public void Stop()
{
m_alive=false;
Thread.Sleep(1000);
}

And in form1.Closing Event of the form which refrence the class,
if (threadClass.Alive) threadClass.Stop();

Also,I wonder this solution althrough is seems working well.

----------------------------------------------------------------------------------------
In my formerly project,I use WinApi with Delp The best solution is like
this,

//the procedure of thread
function ThreadProc(const p:Pointer):Cardinal;stdcall;
begin
while WaitMessage do while PeekMessage(m,0,0,0,PM_REMOVE) do begin
case m.message of
WM_QUIT:Exit;
WM_OTHER_MSG:....
end
end;

//when i want to terminal this thread ,just post a message to thread
PostThreadMessage(FThrID,WM_QUIT,0,0);
//where FThrId is the ID of the Thread

How and we do in c#?
How and we post a messge to a thread so that the threadproc can do a certain
thing ?

Waiting for Expert'' Response.


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

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