C# 多线程和线程同步
我已经编写了以下代码。我创建了两个线程ItmTh和RelTh。 应该首先执行ItmTh线程,当ItmTh线程完成其执行时,应该执行第三个线程。可能的解决方案是什么 `C# 多线程和线程同步,c#,multithreading,C#,Multithreading,我已经编写了以下代码。我创建了两个线程ItmTh和RelTh。 应该首先执行ItmTh线程,当ItmTh线程完成其执行时,应该执行第三个线程。可能的解决方案是什么 ` string[]filepath=Directory.GetFiles(folderPath,“ARAS_IT*”); bool isTemplateFile=true; int arasDefinitionFileCount=filepath.Length; TotalTemplateFile=arasDefinitionFil
string[]filepath=Directory.GetFiles(folderPath,“ARAS_IT*”);
bool isTemplateFile=true;
int arasDefinitionFileCount=filepath.Length;
TotalTemplateFile=arasDefinitionFileCount+Directory.GetFiles(folderPath,“ARAS_Rel*”).Length;
//progressBar1.值=0;
//int progess=0;
if(arasDefinitionFileCount<1)
{
isTemplateFile=false;
//Show(“根文件夹不包含模板文件”);
//返回;
}
ManualResetEvent syncEvent=新的ManualResetEvent(错误);
线程ItmTh=新线程(()=>
{
//迭代根文件夹中的项目类型文件
对于(int i=0;i
{
processDocumentDataDefinitionFile(文件名,folderPath+“\\ARAS\u IT-”+ItemType+“-files\\”;
});
th.Start();
//th.Join();
}
其他的
{
线程th=新线程(()=>
{
processDataDefinitionFile(文件名,tbRootFolderPath.Text,ItemType,folderPath+“\\ARAS\u IT-“+ItemType+”-files\\”;
});
th.Start();
//等待上一个线程完成其任务
//th.Join();
}
}
});
ItmTh.Start();
/*******************************************************************************************************************/
//处理关系文件
//ItmTh.Join();
线程相关性=新线程(()=>
{
syncEvent.WaitOne();
filepath=null;
filepath=Directory.GetFiles(folderPath,“ARAS_Rel*”);
arasDefinitionFileCount=filepath.Length;
if(arasDefinitionFileCount<1&&isTemplateFile==false)
{
Show(“根文件夹不包含模板文件”);
返回;
}
//迭代根文件夹中的关系文件
对于(int i=0;i
{
Cursor.Current=Cursors.WaitCursor;
processDataDefinitionFile(文件名,tbRootFolderPath.Text,ItemType,folderPath+“\\ARAS\u IT-“+ItemType+”-files\\”;
});
th.Start();
//等待上一个线程完成其任务
//th.Join();
}
});
RelTh.Start()`
您可以使用任务
而不是线程
-创建多个线程只是一种开销-您的计算机很可能没有那么多CPU核心<代码>任务和任务并行库
尝试确保根据硬件运行适当数量的线程
//... your code ...
Task ItmTask = new Task.Factory.StartNew(()=>
{
Task[] subTasks = new Task[arasDefinitionFileCount];
for (int i = 0; i < arasDefinitionFileCount; i++)
{
//... your code...
if (ItemType.Equals("Document", StringComparison.InvariantCultureIgnoreCase))
{
subTasks[i] = new Task.Factory.StartNew(() =>
{
processDocumentDataDefinitionFile(fileName, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");
});
}
else
{
subTasks[i] = new Task.Factory.StartNew(() =>
{
processDataDefinitionFile(fileName, tbRootFolderPath.Text, ItemType, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");
});
}
}
// Continue when all sub tasks are done
Task.Factory.ContinueWhenAll(subTasks, _ =>
{
// Cursor.Current = Cursors.WaitCursor;
// .... your code ....
Task[] subTasks2 = new Task[arasDefinitionFileCount];
for (int i = 0; i < arasDefinitionFileCount; i++)
{
subTasks2[i] = new Task.Factory.StartNew(() =>
{
processDataDefinitionFile(fileName, tbRootFolderPath.Text, ItemType, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");
});
}
Task.Factory.ContinueWhenAll(subTasks2, __ =>
{
// reset cursor to normal etc.
});
});
});
/。。。你的代码。。。
任务ItmTask=新建任务.工厂.开始新建(()=>
{
任务[]子任务=新任务[arasDefinitionFileCount];
对于(int i=0;i
{
processDocumentDataDefinitionFile(文件名,文件夹
//... your code ...
Task ItmTask = new Task.Factory.StartNew(()=>
{
Task[] subTasks = new Task[arasDefinitionFileCount];
for (int i = 0; i < arasDefinitionFileCount; i++)
{
//... your code...
if (ItemType.Equals("Document", StringComparison.InvariantCultureIgnoreCase))
{
subTasks[i] = new Task.Factory.StartNew(() =>
{
processDocumentDataDefinitionFile(fileName, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");
});
}
else
{
subTasks[i] = new Task.Factory.StartNew(() =>
{
processDataDefinitionFile(fileName, tbRootFolderPath.Text, ItemType, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");
});
}
}
// Continue when all sub tasks are done
Task.Factory.ContinueWhenAll(subTasks, _ =>
{
// Cursor.Current = Cursors.WaitCursor;
// .... your code ....
Task[] subTasks2 = new Task[arasDefinitionFileCount];
for (int i = 0; i < arasDefinitionFileCount; i++)
{
subTasks2[i] = new Task.Factory.StartNew(() =>
{
processDataDefinitionFile(fileName, tbRootFolderPath.Text, ItemType, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");
});
}
Task.Factory.ContinueWhenAll(subTasks2, __ =>
{
// reset cursor to normal etc.
});
});
});