private void button1_Click(object sender, EventArgs e)
{
List<string> ncmFile=new List<string>();
ncmFile.Add("NCM1");
ncmFile.Add("NCM2");
List<string> naeFile = new List<string>();
naeFile.Add("NAE1");
naeFile.Add("NAE2");
for (int i = 0; i < ncmFile.Count; i++)
{
Thread t = new Thread(new ThreadStart( ()=>NCMNAEConversion(ncmFile[i], naeFile[i])) );
t.Start();
}
}
public void NCMNAEConversion(string ncmFileName, string naeFile)
{
//conversion process
}
注意:示例代码在上面,我想基于no ncm List同时运行NCM NAEConversion方法。这个线程同时运行吗?
答案 0 :(得分:2)
查看Parallel
课程:
http://msdn.microsoft.com/de-de/library/system.threading.tasks.parallel%28v=vs.110%29.aspx
正如Gdoron所说,没有真正的并行处理,但是这个类CPU将为每个线程分配一段时间,然后步骤到下一个,依此类推......
答案 1 :(得分:1)
考虑TPL而不是自己启动线程:
using System.Collections.Generic;
using System.Threading.Tasks;
List<string> ncmFile = new List<string>();
ncmFile.Add("NCM1");
ncmFile.Add("NCM2");
List<string> naeFile = new List<string>();
naeFile.Add("NAE1");
naeFile.Add("NAE2");
Parallel.For(0, ncmFile.Count, (i) =>
{
string ncm = ncmFile[i];
string nae = naeFile[i];
NCMNAEConversion(ncm, nae);
});
答案 2 :(得分:0)
我建议使用ThreadPool创建多个线程。
ThreadPool.QueueUserWorkItem(new WaitCallback(NCMNAEConversion), new string[]{ ncmFile[i], naeFile[i]});
public void NCMNAEConversion(object state)
{
//conversion process
string[] values = (string[])state;
}