如何共享工作线程和主线程的数据?

时间:2014-08-22 07:20:17

标签: .net multithreading

我必须创建几个工作线程,如:

WorkerThreads = new List<Thread>();
WorkerThreadsParams = new List<ThreadSharedValueType>();

foreach (var connection in findDocumentsCountConnections)
{
    var t = new Thread(WorkerThreadFunc);
    var prm = new ThreadSharedValueType() { ... } 
    WorkerThreads.Add(t);
    WorkerThreadsParams.Add(prm);
    t.Start(prm);
}
...

一些用法:

WorkerThreadFunc(prm)
{
  ... heavy initialization here ... 
  prm.Initialized.Set();
  ...
}

MainThread()
{
   ... wait untill each worker thread is initialized 

   foreach (var prm in WorkerThreadsParams) 
     prm.Initialized.WaitOne();
}

我猜应该有更优雅的2个名单?

0 个答案:

没有答案