它不需要删除线程对象 [英] doesn't it need to delete thread object

查看:75
本文介绍了它不需要删除线程对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的项目中有一个搜索主题。线程在'Form1()'函数中创建:

Hi,
i have a search thread in my project . the thread is created in 'Form1()' function:

objSearchThread = new Thread(this.Thread_Func);





当用户点击搜索按钮时,调用Start()函数:



when user clicks the 'search' button, Start() function is called:

private void Button_Search_Click(object sender, EventArgs e)
{
    objSearchThread.Start();
}





第二次点击此按钮,崩溃了!因为线程处于'已启动'状态。如果我更改我的按钮单击代码,我添加'新'命令。它没有错误或崩溃:



second clicked of this button, crashed! because the thread is 'Started' state. if i change my button clicked code, and i add 'new' command. it works without error or crashing:

private void Button_Search_Click(object sender, EventArgs e)
{
    objSearchThread = new Thread(this.Thread_Func);
    objSearchThread.Start();
}





是不是需要删除线程对象(objSearchThread)?当线程工作结束时,是否需要调用Abort()或其他函数?我在这里写的第二个代码是正确的吗?



doesn't it need to delete thread object(objSearchThread )? does it need to call Abort() or other functions, when thread working ends? is second code that i write here correct?

推荐答案

Rob对正在运行的线程是正确的,但每次用户点击搜索时都没有创建新线程的问题按钮,因为我想这个线程会搜索一些东西,然后快速结束,对吧?因此没有创建数百个非终止线程的风险。



有两个选项:

1.创建一个独特的线程,使用一些信号告诉何时开始搜索,何时完成以及何时退出线程。

2.每次点击创建一个线程(就像你已经做过的那样)。



选项2当然简单得多,适合你的情况。



另外,我建议你有一个查看 BackgroundWorker 类。这个类提供了后台线程的简单使用,可以报告进度,并支持取消。



如果你想要一个现成的用户界面,你可以想看看那里: ProgressForm:一个简单的表格链接到BackgroundWorker [ ^ ]
Rob is right about the running threads but there is no problem about creating a new thread each time the user clicks the search button because I suppose this thread will search for something and then end quickly, right? So there is no risk of creating hundreds of non-terminating threads.

There are 2 options:
1. create a unique thread and use some signaling to tell when to start the search, when it is done, and when to quit the thread.
2. create one thread per click (like you already did).

Option 2 is much simpler of course and is suitable for your case.

Additionnaly, I would recommand you to have a look to BackgroundWorker class. This class provides simple use of a background thread, can report progress, and supports cancelation.

And if you want a ready-to-use UI for that, you may want to have look there: ProgressForm: A simple form linked to a BackgroundWorker[^]


如你所说,你不能为第一个例子开始两次线程。



在第二个按钮中,每次单击该按钮都会创建一个新线程并将其设置为运行 - 这听起来很糟糕,您不希望用户能够创建无限数量的线程。



正在运行的线程不会收集垃圾,即使你不再引用它,所以你需要告诉它停止。你可以使用Abort(),但这是一个丑陋的解决方案。更好的方法是使用一些信令告诉线程何时终止,从而允许它进行所需的任何清理操作。
Well as you say, you can't start a thread twice for the first example.

In the second, on every click of the button you create a new thread and set it running - this sounds bad, you wouldn't want a user to be able to create an unlimited number of threads.

A running thread won't get garbage collected, even if you no longer have references to it, so you would need to tell it to stop. You can use Abort() for this but its an ugly solution. A better way is to use some signaling to tell the thread to terminate when it can, thus allowing it to do any clear up operations it needs.


这篇关于它不需要删除线程对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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