我目前正在执行一个耗费大量时间执行的任务。因此,我选择了线程。但我在我想创建多个线程的线程中有一个foreach循环。 我很担心这是做这件事的恰当方法。每个循环的多个线程
我的代码是类似以下内容:
Thread th= new Thread(new ThreadStart(ThreadProcedure));
th.IsBackground = true;
th.Start();
public void ThreadProcedure()
{
//I have datatable here
foreach(DataRow in mytable.rows)
{
//here I want to create a multiple threads, say like
//Thread1 on which I want to run method1
Method1(dr["Column1"].ToString());
//Thread2 on which I want to run method2
Method2(dr["Column2"].ToString());
//Thread3 on which I want to run method3
Method3(dr["Column3"].ToString());
}
}
里面我的foreach我通过将单元格的值在数据行上运行的一些方法。
你有什么问题 –
你一般都想在循环的外面创建你的线程,然后给每一个线程做部分工作。正如马修所说,Parallel.ForEach是为此而设计的。 – EricLaw
@ DanielA.White我正在尝试执行parallel.foreach,它比foreach执行速度快得多,但无法在我的数据表的datarows上实现它。 – Nagaraj