任何重启终止线程的方法 [英] Any way to restart a terminated thread

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

问题描述

有没有办法在不使用池的情况下启动已终止的线程或

创建新的线程对象?


void counter()

{

clicks = 0;

clock.Start();

while(counterActive)

{

点击++;

Thread.SpinWait(counterWaits);

}

clock.Stop() ;

}


双点PointProfiler(int等待,int睡眠)

{

counterWaits =等待;

而(counterThread.IsAlive)counterActive = false;

counterActive = true; clicks = 0;

counterThread.Start();

Thread.Sleep(sleep);

while(counterThread.IsAlive)counterActive = false ;

返回点击次数/时钟。时间;

}

我希望每次都能启动计数器线程但不能创建新的

线程(因为性能开销)。我也不希望计数器

永远运行所以我需要一种重启它的方法。看来这是不可能的?b $ b不可能? ; /


有没有办法让线程重新进入?我试图将
暂停,但暂停已经过时且无法正常工作。如果我这样做.Start()然后

..Suspend()就在它之后我得到100%的cpu使用量,好像它没有暂停

任何东西。 />

似乎我别无选择,只能使用游泳池,但我认为它可能会导致一些性能问题(我正在尝试将时间作为时间问题)准确的

可能)。我可以做我想做的事情,如果我可以让计数器线程连续运行

但是现在因为我正在尝试将它作为gui的一部分整合我

只需要让它在需要时运行。 (我认为每次我需要这样做时创建一个新的
线程都是出于追求(因为我会连续几次进行
分析) )。

Is there any way to start a terminated thread without using a pool or
creating a new thread object?

void counter()
{
clicks = 0;
clock.Start();
while (counterActive)
{
clicks++;
Thread.SpinWait(counterWaits);
}
clock.Stop();
}

double PointProfiler(int waits, int sleep)
{
counterWaits = waits;
while (counterThread.IsAlive) counterActive = false;
counterActive = true; clicks = 0;
counterThread.Start();
Thread.Sleep(sleep);
while (counterThread.IsAlive) counterActive = false;
return clicks / clock.Time;
}
I want to be able to start the counter thread each time but not create a new
thread(because of the performance overhead). I also don''t want the counter
to run for ever so I need a way of restarting it. It seems that this is
impossible though? ;/

Is there any way to make it so that the thread is re-entrant? I tried to
suspend but suspend is obsolete and isn''t working. If I do .Start() then
..Suspend() right after it I get 100% cpu usage as if it didn''t suspend
anything.

It seems that I have no choice but to use a pool but I think that it might
cause some performance issues(I''m trying to time things as accurate as
possible). I can do what I want to do if I can let the counter thread run
continuously but now since I''m trying to integrate it as part of a gui I
need to only let it run when its needed. (and I think that creating a new
thread every time I need to do this is out of the questing(since I''ll be
profiling several times in a row).

推荐答案

顺便说一句,我不希望代码是异步的。我正在尝试分析

线程的绝对时间并且需要最小化其他线程

干扰。(我不需要它在后台而且不在乎它是否

暂时冻结电脑)


我也在没有线程的主循环中做了这段代码但问题是

我最终从我的时钟例程中计算多余的时间。(基本上每次点击都会产生
时钟并且它会引入重大错误。我想我可以

做一个平均值,但已经取了一堆平均...基本上

我的旧代码使用一个线程我可以使用

Thread.SpinWait(1)...使用我的新代码主循环我使用SpinWait(1)得到最大值

250khz。)如果我找不到任何令人满意的方式使用

线程那么我会尝试改进我的主循环代码。

BTW, I don''t want the code to be asynchronous. I am trying to profile the
absolute timing of the thread and need to minimize other thread
interference. (and I don''t need it in the background and don''t care if it
freezes up the computer momentarily)

I also did this code in the main loop without a thread but the problem is
that I end up counting excess time from my clock routines. (Basically
clocking every click and it introduces significant error . I guess I could
do an average over it but already taking a bunch of averages... basically
with my old code using a thread I could ge around 10mhz using
Thread.SpinWait(1)... using my new code in the main loop I get max about
250khz using SpinWait(1).) If I can''t find any satisfactory way to use
threads then I''ll try to improve my main loop code.




" Jon Slaughter" < Jo *********** @ Hotmail.com写信息

新闻:CY ****************** @ newssvr11.news.prodigy.n et ...

"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:CY******************@newssvr11.news.prodigy.n et...

有没有办法在不使用池的情况下启动已终止的线程或

创建一个新的线程对象?
Is there any way to start a terminated thread without using a pool or
creating a new thread object?



为什么不使用手动重置事件?只要事件

仍然设置,线程就会运行。

Why not use a manual-reset event? The thread will run as long as the event
remains set.


>

void counter()

{

clicks = 0;

clock.Start();

while(counterActive)

{

点击++;

Thread.SpinWait(counterWaits);

}

时钟。停止();

}


双点PointProfiler(int等待,int睡眠)

{

counterWaits =等待;

while(counterThread.IsAlive)counterActive = false;

counterActive = true; clicks = 0;

counterThread.Start();

Thread.Sleep(sleep);

while(counterThread.IsAlive)counterActive = false ;

返回点击次数/时钟。时间;

}


我希望每次都可以启动计数器线程但是没有创建一个新的线程(由于性能开销)。我也不希望

计数器永远运行,所以我需要一种重启它的方法。看来

虽然这是不可能的? ; /


有没有办法让线程重新进入?我试图将
暂停,但暂停已经过时且无法正常工作。如果我这样做.Start()然后

.Suspend()就在它之后我得到100%的cpu使用量,好像它没有暂停

任何东西。


似乎我别无选择,只能使用游泳池,但我认为它可能会导致一些性能问题(我正在尝试将时间准确定位) as

可能)。我可以做我想做的事情,如果我可以让计数器线程连续运行

但是现在因为我正在尝试将它作为gui的一部分整合我

只需要让它在需要时运行。 (我认为每次我需要这样做时创建一个新的
线程都是出于追求(因为我会连续几次进行
分析) )。
>
void counter()
{
clicks = 0;
clock.Start();
while (counterActive)
{
clicks++;
Thread.SpinWait(counterWaits);
}
clock.Stop();
}

double PointProfiler(int waits, int sleep)
{
counterWaits = waits;
while (counterThread.IsAlive) counterActive = false;
counterActive = true; clicks = 0;
counterThread.Start();
Thread.Sleep(sleep);
while (counterThread.IsAlive) counterActive = false;
return clicks / clock.Time;
}
I want to be able to start the counter thread each time but not create a
new thread(because of the performance overhead). I also don''t want the
counter to run for ever so I need a way of restarting it. It seems that
this is impossible though? ;/

Is there any way to make it so that the thread is re-entrant? I tried to
suspend but suspend is obsolete and isn''t working. If I do .Start() then
.Suspend() right after it I get 100% cpu usage as if it didn''t suspend
anything.

It seems that I have no choice but to use a pool but I think that it might
cause some performance issues(I''m trying to time things as accurate as
possible). I can do what I want to do if I can let the counter thread run
continuously but now since I''m trying to integrate it as part of a gui I
need to only let it run when its needed. (and I think that creating a new
thread every time I need to do this is out of the questing(since I''ll be
profiling several times in a row).





Ben Voigt [C ++ MVP]"< rb * @ nospam。 nospamwrote in message

news:e6 **************** @ TK2MSFTNGP03.phx.gbl ...

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:e6****************@TK2MSFTNGP03.phx.gbl...

>

" Jon Slaughter" Jo *********** @ Hotmail.com写信息

新闻: CY******************@newssvr11.news.prodigy.n et ...
>
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:CY******************@newssvr11.news.prodigy.n et...

> Is有没有办法在不使用池的情况下启动已终止的线程或者创建新的线程对象?
>Is there any way to start a terminated thread without using a pool or
creating a new thread object?



为什么不使用手动重置事件?只要

事件仍然存在就会运行。


Why not use a manual-reset event? The thread will run as long as the
event remains set.



我不确定你在谈论什么?能否解释一下很少

更多?我需要这个广告可以重新进入但不能连续运行
。基本上我需要像函数调用一样使用它,但是在一个不同的线程上使用它。 (因为我需要花费多长时间来运行

异步方式)。

I''m not sure what your talking about? Could you please explain a little
more? I need the thread to be able to be re-entrant but not run
continuously. Essentially I need to use it like a function call but on a
different thread. (Because I need to time how long it takes to run in an
asychronous way).


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

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