大家好,我有几个问题,为什么会发生这些事情。首先,我设置了numOfAsynchEx等于的许多进程的初始触发。这应该启动两个不同的线程,它们有两个不同的ExecuteThread类实例,并且有两个SAME方法(ThreadProcess),但不同的变量是否正确?
int numOfAsynchEx = 2;
for (int i = 0; i < numOfAsynchEx; i++)
{
if (entryQueue.Count > 0)
{
ExecuteThread eT = new ExecuteThread(entryQueue.Dequeue()
new startNextThread(startNextThread));
Thread newThread = new Thread(eT.ThreadProcess);
newThread.Start();
metrics.TotalAttempted++;
metrics.ThreadsRunning++;
}
}
这是同一类中的回调方法:
private void startNextThread(ParsingInfo info)
{
metrics.ThreadsRunning--;
if (entryQueue.Count > 0)
{
metrics.TotalAttempted++;
ExecuteThread eT = new ExecuteThread(entryQueue.Dequeue()
new startNextThread(startNextThread));
Thread newThread = new Thread(eT.ThreadProcess);
newThread.Start();
metrics.ThreadsRunning++;
}
else if(metrics.ThreadsRunning == 0)
{
ThreadsDone = true;
}
}
编辑这是执行线程类 公共类ExecuteThread {
private CatalogEntry entry;
private startNextThread callBackDelegate;
private ProcessStartInfo startInfo;
private ParsingInfo parseInfo;
public ExecuteThread(CatalogEntry entry, startNextThread callBack)
{
parseInfo = new ParsingInfo();
this.entry = entry;
callBackDelegate = callBack;
createStartInfo();
InstantiateProcess();
}
private void createStartInfo()
{
startInfo = new ProcessStartInfo(exePath);
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
//startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = + filePath + " " ;
}
private void InstantiateProcess()
{
fileParserExe = new Process();
fileParserExe.StartInfo = startInfo;
fileParserExe.EnableRaisingEvents = true;
}
private void Parse()
{
try
{
this.fileParserExe.Start();
this.fileParserExe.WaitForExit();
parseInfo.additionalMessage += fileParserExe.StandardOutput.ReadToEnd();
}
catch (Exception e)
{
parseInfo.additionalMessage += e.ToString();
parseInfo.additionalMessage += "Could not locate single file parser executable: " + exePath;
}
}
public void ThreadProcess()
{
this.parseInfo.fileName = entry.fileName;
this.parseInfo.startTime = DateTime.Now;
Parse();
this.parseInfo.endTime = DateTime.Now;
this.parseInfo.SetElapsedTime();
if (this.callBackDelegate != null)
{
this.callBackDelegate(this.parseInfo);
}
}
}
由于某种原因,这不会同时触发两个线程并异步执行它们。它一次做一个。我不明白为什么。有人可以开导我吗?
编辑:在玩了一段时间之后,当我注释掉redirectStandardOutput和useShellExecute时,程序就像过去一样完美地工作。我现在的问题是如何重定向输出,因为我显然无法这样做?
答案 0 :(得分:2)
只是一些建议......