问题描述
由于一个线程没有Stop功能,我不习惯使用中止,
我想知道我是如何阻止一个线程的?
我的问题是我只想在后台执行一个函数并且
可能重启它。
基本上是做什么的:
if(SerialRowThread!= null)
{
SerialRowThread.Abort();
WaitingThread.Stop();
}
WaitingThread.Start();
SR.Setup(domainUpDown1.SelectedIndex,Intervals,Entries) ;
SerialRowThread =新线程(SR.Compute);
SerialRowThread.Start();
----
SR是包含方法compute和SR.Setup设置
状态的类。
现在这样可行,但我想知道我是否可以暂停线程并且它
会做同样但不使用中止。
即,GC将最终处置该线程,因为我重新分配
SerialRowThread。因此,希望它本来可以在线程中调用中止
或者做任何干净的假设。
这个方法是否正常(即调用suspend)而不是在上面的代码中中止)
或我是否需要使用一些池化线程的方法?我仍然不清楚线程如何关闭,因为即使在合并中它也必须完成?
谢谢,
Jon
Since a thread doesn''t have a Stop feature and I''m not supose to use abort,
I''m wondering how I stop a thread?
My problem is that I simply want to excute a function in the background and
possibly "restart" it.
What''ve been doing is essentially:
if (SerialRowThread != null)
{
SerialRowThread.Abort();
WaitingThread.Stop();
}
WaitingThread.Start();
SR.Setup(domainUpDown1.SelectedIndex, Intervals, Entries);
SerialRowThread = new Thread(SR.Compute);
SerialRowThread.Start();
----
SR is the class that contains the method compute and SR.Setup sets up the
state.
now this works but I''m wondering if I can just suspend the thread and it
will do the same but not use abort.
i.e., the GC will end up disposing of the thread because I reassign
SerialRowThread. So it will, hopefully, inherently call abort on the thread
or do whatever clean its suppose to do.
Is this method ok(i.e., calling suspend instead of abort in the above code)
or do I need to use some method of pooling threads? I''m still not to clear
on how a thread shuts down because even in pooling it must be done?
Thanks,
Jon
推荐答案
线程计算一些东西。为简单起见,我们假设它是计算两个数字之间的素数。现在用户选择gui
中的数字并可能决定更改它们(假设计算需要很长时间
足够这样做,以至于在他决定之前没有完成改变)。
所以我不想开始计算新素数的另一个线程,而旧的素数正在继续。我也不想暂停原来的那个因为
然后它在中间停止它...它没有免费资源或任何东西。
我基本上想调用GeneratePrimes(a,b);当用户想要看到他们的b $ b,但我需要在后台进行,这样用户就可以做一些其他的
的东西了。比如没有必要让用户等待10分钟(假设)如果
他意识到他使用间隔[10,1000]而不是[10,100]
这就是他所放入的。所以他要么等待,要么关闭应用程序。
当他进入新的时间间隔开始新的计算时,似乎更好
和旧的结束,结果将被忽略,因为用户
不想要它们。
一个问题,这不是问题我有它设置的方式,
线程实际上是如何工作的。我认为这被称为重新进入,我的意思是
当另一个线程
开始新计算而不停止计算时,该函数可能处于计算中间原始线程。如果它的
没有正确编程,那么可能会发生不好的事情。我想这就是为什么
锁和东西很重要。但有一点我想知道的是,如果线程
以某种方式将代码复制到一个新块中,那么所有本地数据都是重复的。
虽然我认为那些东西与公寓模型或其他什么有关?
主要的是我只需要停止线程和重启它在新的
参数上,但在我读到的任何地方都说中止不是可行的方式
线程没有停止方法。所以也许停止是由线程暂停
,然后没有引用它以便GC清理它b
?但是suspend()在2.0中折旧; /似乎我唯一剩下的就是
剩下的就是中止; /
谢谢,
Jon
The thread computes some stuff. For simplicity lets suppose it is to compute
the primes between two numbers. Now the user selects the numbers in a gui
and might decide to change them(lets suppose the computation takes long
enough to do so that its not finished before he decides to change).
So I don''t want to start another thread that computes the new primes while
the old one is going on. Neither do I want to suspend the old one because
then it stops it in the middle... it doesn''t free resources or anything.
I basically want to call GeneratePrimes(a,b); when the user wants to see
them but I need to do it in the background so the user can do some other
stuff. Theres no need, say, for the user to wait 10 mins(hypothetically) if
he realizes that he ment to use the interval [10,1000] instead of [10,100]
which is what he put in. So he either waits or closes the app.
Seems better that when he enters a new interval a new computation is started
and the old one is ended and the results would be disregarded since the user
didn''t want them.
One issue, which isn''t a problem the way I have it setup, is how the
threads actually work. I think this is called re-entry, what I mean is that
the function might be in the middle of a computation when another thread
starts a new computation without stopping the original thread. Here if its
not programmed properly then bad things could happen. I suppose this is why
locks and stuff are important. But one thing I''m wondering is if the thread
somehow copies the code to a new block so all local data is duplicated.
Although I think that stuff has to do with the apartment model or whatever?
Main thing is I just need to "stop" the thread and "restart" it on new
parameters but everywhere I read says that abort is not the way to go yet
there is no stop method for threads. So maybe the stop is implied by a
suspension of the thread and then no reference to it so that the GC cleans
it up? But the suspend() is depreciated in 2.0 ;/ Seems the only thing I''m
left with is abort ;/
Thanks,
Jon
这篇关于线程结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!