【发布时间】:2011-10-05 05:33:52
【问题描述】:
我试图使用多线程处理一些数据,其中我使用线程池来生成等于内核数的线程(仅限于 .NET 2.0 到 3.5,因此不能使用 TPL)。
我的所有线程都在我的数据的不同部分执行相同的函数。
我的问题是我无法从线程池的线程更新进度条。找到了invoke和Background worker之类的解决方案,但是不太明白怎么用,请帮我看看怎么用。
我的代码看起来像
private static float[] inpdat=new float[1000];//The input array to process
//The spawned threads
public void dowork(object o)
{
int np = (int)o;
for(int i=np;i<inpdat.length;i=i+Environment.ProcessorCount)
{
//do some processing
if(np==0)
{
//update progress bar here
}
}
}
//The main thread
for (int npou = 0; npou < numproc; npou++)
{
resetEvents[npou] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(dowork), npou);
PetrelLogger.InfoOutputWindow("manual reset event set");
}
速度对我来说是最重要的,所以如果跨线程调用占用最少的时间会非常有帮助。 谢谢
【问题讨论】:
-
“使用线程池生成与内核数相等的线程” - 您很少需要自己明确地优化线程数。线程池就是为此而设计的。
标签: c# progress-bar threadpool