C#我想线程安全有问题吧?
大家好,我有几个问题要问为什么会发生这些事情。首先,我有一个初始启动,不管您将numOfAsynchEx设置为多少个进程。这应该用ExecuteThread类的两个不同实例启动两个不同的线程,并且有两个相同的方法(ThreadProcess),但变量不同,对吗C#我想线程安全有问题吧?,c#,multithreading,C#,Multithreading,大家好,我有几个问题要问为什么会发生这些事情。首先,我有一个初始启动,不管您将numOfAsynchEx设置为多少个进程。这应该用ExecuteThread类的两个不同实例启动两个不同的线程,并且有两个相同的方法(ThreadProcess),但变量不同,对吗 int numOfAsynchEx = 2; for (int i = 0; i < numOfAsynchEx; i++) { if (entryQueue.Count &g
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++;
}
}
编辑这里是执行线程类
公共类ExecuteThread
{
出于某种原因,这不是同时启动两个线程并异步执行它们。而是一次执行一个线程。我不明白为什么。有人能告诉我吗
编辑:在玩了一段时间后,当我注释掉redirectStandardOutput和useShellExecute时,该程序与以前一样工作正常。我现在的问题是如何重定向输出,因为我显然不能这样做?只是一些建议
- 什么是entryQueue?普通队列{T}不是线程安全的
- 自从entryQueue.Count和 entryQueue.Dequeue()不是原子地一起完成的
- 您可能想使用Interlocked.Increment和decreation代替 普通的++和--
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;
}
}
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);
}
}
}