如何杀死一个线程? [英] How to kill a thread?

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

问题描述



我有一个Windows窗体应用程序,表单中有2个按钮。在

第一个按钮'的点击事件中,我的代码如下:

线程t =新线程(新的ThreadStart(fileProcessor));

t.Start();

运行冗长的处理。


我希望当我点击第二个按钮时我可以手动终止此线程。


我该怎么办?


谢谢

哈代

Hi,
I have a windows form application, and there are 2 buttons in the form. In
first button''s click event I have code like:
Thread t = new Thread(new ThreadStart(fileProcessor));
t.Start();
To run a lengthy processing.

I hope I can manully terminate this thread when I click the second button.

How can I do it?

Thanks
Hardy

推荐答案

嗨Hardy,


一种方法是调用Thread.Abort()方法,但更好的方法是使用

某种同步机制,例如ManualEvent。


-

Miha Markic [MVP C#] - RightHand .NET咨询&开发

miha at rthand com
www.rthand.com


" Hardy Wang" <公顷******* @ hotmail.com>在消息中写道

新闻:Oi ************** @ TK2MSFTNGP10.phx.gbl ...
Hi Hardy,

One way would be to call Thread.Abort() method, though better way is to use
some sort of synchronization mechanims, such as ManualEvent.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Hardy Wang" <ha*******@hotmail.com> wrote in message
news:Oi**************@TK2MSFTNGP10.phx.gbl...
我有一个Windows窗体应用程序,表单中有2个按钮。在第一个按钮'的点击事件中我有类似的代码:
线程t =新线程(新的ThreadStart(fileProcessor));
t.Start();
运行一个冗长的处理。

我希望当我点击第二个按钮时我可以手动终止这个帖子。

我该怎么做?

谢谢
Hardy
Hi,
I have a windows form application, and there are 2 buttons in the form. In
first button''s click event I have code like:
Thread t = new Thread(new ThreadStart(fileProcessor));
t.Start();
To run a lengthy processing.

I hope I can manully terminate this thread when I click the second button.

How can I do it?

Thanks
Hardy



谢谢,

线程的定义出现在第一个按钮的点击事件。在

第二个按钮'的点击事件中,线程定义超出了范围,那么我怎么能在第二个函数中调用Thread.Abort()?b / br / >

Miha Markic [MVP C#]" < miha at rthand com>在消息中写道

news:eS ************** @ tk2msftngp13.phx.gbl ...
Thanks,
The definition of thread appears in first button''s click event. In the
second button''s click event, thread definition is out of scope, so how can I
call Thread.Abort() in second function?

"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:eS**************@tk2msftngp13.phx.gbl...
Hi Hardy,
- <
Miha Markic [MVP C#] - RightHand .NET咨询&开发
miha at rthand com
www.rthand.com

Hardy Wang <公顷******* @ hotmail.com>在消息中写道
新闻:Oi ************** @ TK2MSFTNGP10.phx.gbl ...
Hi Hardy,

One way would be to call Thread.Abort() method, though better way is to use some sort of synchronization mechanims, such as ManualEvent.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Hardy Wang" <ha*******@hotmail.com> wrote in message
news:Oi**************@TK2MSFTNGP10.phx.gbl...

我有一个Windows窗体应用程序,窗体中有2个按钮。
在第一个按钮'的点击事件中我有类似的代码:
线程t =新线程(新的ThreadStart(fileProcessor));
t.Start();
运行一个漫长的处理。

我希望当我点击第二个
按钮时我可以手动终止这个帖子。
我该怎么做?

谢谢
哈迪
Hi,
I have a windows form application, and there are 2 buttons in the form. In first button''s click event I have code like:
Thread t = new Thread(new ThreadStart(fileProcessor));
t.Start();
To run a lengthy processing.

I hope I can manully terminate this thread when I click the second button.
How can I do it?

Thanks
Hardy






" Hardy Wang" <公顷******* @ hotmail.com>在消息中写道

新闻:ew ************** @ TK2MSFTNGP10.phx.gbl ...

"Hardy Wang" <ha*******@hotmail.com> wrote in message
news:ew**************@TK2MSFTNGP10.phx.gbl...
谢谢,线程的定义出现在第一个按钮的点击事件中。在
第二个按钮的click事件中,线程定义超出了范围,那么
如何在第二个函数中调用Thread.Abort()?
Thanks,
The definition of thread appears in first button''s click event. In the
second button''s click event, thread definition is out of scope, so how can I call Thread.Abort() in second function?



不要在第二个按钮的事件中定义它。将其定义为

类的成员。


Don''t define it in the second button''s event. Define it as a member of your
class.


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

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