ThreadPool等待线程完成 [英] ThreadPool wait for threads to finish

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

问题描述

您好


我正在寻找使用线程池,例如Jon Skeet写的

http://www.yoda.arachsys.com/csharp/miscutil/) 。谁能告诉我这个

池是否可以等待所有线程完成?


例如,如果我开始20个线程:


CustomThreadPool pool = new CustomThreadPool(" PetersThreadPool");

ThreadMethod m = new ThreadMethod(InsertThread);

for(int i = 0; i< 20; i ++)

{

pool.AddWorkItem(m,i);

}


[

我的线程方法是:

委托对象ThreadMethod(int val);

私有对象InsertThread (int val)

{

Console.WriteLine(" Thread" + val);

返回0;

}

]


如果所有线程都完成,我的程序应该继续吗?b $ b继续吗?我在这里这样做:

while(pool.WorkingThreads> 0)

{

}


谢谢,

彼得

解决方案

只是一个想法...


完成后每个线程都会向主线程报告,然后检查

该点的线程数,以查看其他线程是否仍在运行....而不是

一段时间的循环

" Peter Kirk" < pk@alpha-solutions.dk>在留言中写道

新闻:ej ************** @ tk2msftngp13.phx.gbl ...

你好

我正在寻找使用线程池,例如由Jon Skeet编写的线程池( http://www.yoda.arachsys.com/csharp/miscutil/) 。任何人都可以告诉我,如果
这个池提供了等待其所有线程完成的可能性吗?

例如,如果我开始20个线程:

CustomThreadPool池= new CustomThreadPool(" PetersThreadPool");
ThreadMethod m = new ThreadMethod(InsertThread);
for(int i = 0; i< 20; i ++)
{
pool.AddWorkItem(m,i);
}

[
我的线程方法是:
委托对象ThreadMethod(int val);
私有object InsertThread(int val)
{
Console.WriteLine(" Thread" + val);
返回0;
}
]

如何在我的程序继续之前等待所有线程完成?我在这里这样做:
while(pool.WorkingThreads> 0)
{
}

谢谢,
Peter



Shardool Karnik< Th ********** @ gmail.com>写道:

让每个线程在完成后报告主线程,然后检查那个点的线程数,看看其他人是否还在运行....而不是
一个循环




那会起作用 - 或者使每个线程生成在一个信号量上,并且

等待(从主线程)直到信号量被击中

适当的次数。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件





如果你想按顺序执行它们你需要为它做一个线程吗?


如果你这样做会怎么样:

1-用正确的代表创建一个队列,引用你想要执行的方法

2 - 创建一个线程并开始弹出代表并依次执行。


唯一的缺点可能是你无法阻止单个项目,你可以

完成线程,但它会完成整个执行,但你可能会发现

执行其余的其他方法。

欢呼,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州运输部


" Peter Kirk" < pk@alpha-solutions.dk>在留言中写道

新闻:ej ************** @ tk2msftngp13.phx.gbl ...

你好

我正在寻找使用线程池,例如由Jon Skeet编写的线程池( http://www.yoda.arachsys.com/csharp/miscutil/) 。谁能告诉我这个池是否有可能等待所有线程完成?

例如,如果我开始20个线程:

CustomThreadPool池= new CustomThreadPool(" PetersThreadPool");
ThreadMethod m = new ThreadMethod(InsertThread);
for(int i = 0; i< 20; i ++)
{
pool.AddWorkItem(m,i);
}

[
我的线程方法是:
委托对象ThreadMethod(int val);
私有object InsertThread(int val)
{
Console.WriteLine(" Thread" + val);
返回0;
}
]

如何在我的程序继续之前等待所有线程完成?我是否在这里这样做:
while(pool.WorkingThreads> 0)
{
}

谢谢,
Peter



Hi there

I am looking at using a thread-pool, for example one written by Jon Skeet
(http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if this
pool provides the possibility to wait for all its threads to finish?

For example, if I start 20 threads:

CustomThreadPool pool = new CustomThreadPool("PetersThreadPool");
ThreadMethod m = new ThreadMethod(InsertThread);
for (int i = 0; i < 20; i++)
{
pool.AddWorkItem(m, i);
}

[
where my thread method is:
delegate object ThreadMethod(int val);
private object InsertThread(int val)
{
Console.WriteLine("Thread " + val);
return 0;
}
]

How do I wait until all the threads are finished, before my program should
continue? Do I do this here:
while (pool.WorkingThreads > 0)
{
}

Thanks,
Peter

解决方案

just a thought ...

have each thread report to the main thread upon completion and then check
the thread count at that point to see if others are still running....instead
of a while loop
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...

Hi there

I am looking at using a thread-pool, for example one written by Jon Skeet
(http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if this pool provides the possibility to wait for all its threads to finish?

For example, if I start 20 threads:

CustomThreadPool pool = new CustomThreadPool("PetersThreadPool");
ThreadMethod m = new ThreadMethod(InsertThread);
for (int i = 0; i < 20; i++)
{
pool.AddWorkItem(m, i);
}

[
where my thread method is:
delegate object ThreadMethod(int val);
private object InsertThread(int val)
{
Console.WriteLine("Thread " + val);
return 0;
}
]

How do I wait until all the threads are finished, before my program should
continue? Do I do this here:
while (pool.WorkingThreads > 0)
{
}

Thanks,
Peter



Shardool Karnik <Th**********@gmail.com> wrote:

have each thread report to the main thread upon completion and then check
the thread count at that point to see if others are still running....instead
of a while loop



That would work - or make each thread "produce" on a semaphore, and
wait (from the main thread) until the semaphore had been hit the
appropriate number of times.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Hi,

If you want to execute them in sequence what you need to make a thread for?

what if you do this:
1- Create a queue with the correct delegates referencing the methods you
want to execute
2- Create a thread and start Pop-ing the delegates and executing in turn.

The only drawback may be that you cannot stop an individual item, you could
finish the thread but it would finish the entire execution, but you may find
other approach to execute the rest.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...

Hi there

I am looking at using a thread-pool, for example one written by Jon Skeet
(http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if
this pool provides the possibility to wait for all its threads to finish?

For example, if I start 20 threads:

CustomThreadPool pool = new CustomThreadPool("PetersThreadPool");
ThreadMethod m = new ThreadMethod(InsertThread);
for (int i = 0; i < 20; i++)
{
pool.AddWorkItem(m, i);
}

[
where my thread method is:
delegate object ThreadMethod(int val);
private object InsertThread(int val)
{
Console.WriteLine("Thread " + val);
return 0;
}
]

How do I wait until all the threads are finished, before my program should
continue? Do I do this here:
while (pool.WorkingThreads > 0)
{
}

Thanks,
Peter



这篇关于ThreadPool等待线程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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