gpt4 book ai didi

c# - 任何人都可以看到我重定向这些进程的输出的方式有问题吗?

转载 作者:行者123 更新时间:2023-12-03 13:21:51 24 4
gpt4 key购买 nike

好吧,所以我在线程方面遇到问题。我正在实例化一个新的“ExecuteThread”类,并启动一个新线程以异步多次运行该新引用中的“ThreadProccess”方法。现在所有的过程都完美地异步启动、运行和完成。问题是,我需要重定向这些进程的输出。一旦我取消注释那里注释的行,一切都会变得异常并开始连续运行,并返回错误的耗时等。 (之前耗时是正确的)。我需要这条线,因为它是存储输出的地方。任何人都可以看到我重定向这些输出的方式有问题吗?

public class ExecuteThread
{
private string exePath;
private string gPath;
private string filePath;
private string tPath;

private Dictionary<string, string> gReference;
private Entry entry;
private startNextThread callBackDelegate;
private ProcessStartInfo startInfo;
private PInfo pInfo;
private Process filePExe;

public ExecuteThread(Entry entry, string exe, Dictionary<string, string> gReference, startNextThread callBack)
{
pInfo = new PInfo();
this.entry = entry;
this.gReference = gReference;
exePath = exe;
callBackDelegate = callBack;

getPInfo();
createStartInfo();
InstantiateProcess();
}

private void getPInfo()
{
GetGPath();
filePath = entry.ResolveSourcePath(entry);
tPath = entry.ResolveArtifactsTagsPath(entry);
}

private void GetGPath()
{
if (!string.IsNullOrEmpty(entry.fileType))
{
if (g.ContainsKey(entry.fileType))
gPath = gReference[entry.fileType];
return;
}
gPath = null;
}

private void createStartInfo()
{
startInfo = new ProcessStartInfo(exePath);
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = gPath + " " + filePath + " " + tPath;
}

private void InstantiateProcess()
{
filePExe = new Process();
filePExe.StartInfo = startInfo;
filePExe.EnableRaisingEvents = true;
}
private void Parse()
{
try
{
this.filePExe.Start();
this.fileParserExe.WaitForExit();
//this.pInfo.additionalMessage += filePExe.StandardOutput.ReadToEnd();
this.filePExe.Close();
}
catch (Exception e)
{
parseInfo.additionalMessage += e.ToString();
parseInfo.additionalMessage += "Could not locate single file p executable: " + exePath;
}

}

public void ThreadProcess()
{
this.pInfo.fileName = entry.fileName;
this.pInfo.startTime = DateTime.Now;
Parse();
this.pInfo.endTime = DateTime.Now;
this.pInfo.SetElapsedTime();

if (this.callBackDelegate != null)
{
this.callBackDelegate(this.pInfo);
}
}
}

public class PInfo
{
public string fileName;
public DateTime startTime;
public DateTime endTime;
public string ElapsedTime;
public string additionalMessage = "";

public void SetElapsedTime()
{
TimeSpan elapsedTime;
elapsedTime = this.endTime.Subtract(this.startTime);
ElapsedTime = string.Format("{0:hh\\:mm\\:ss.fff}", elapsedTime);
}

}
//How I'm starting each thread
if (entryQueue.Count > 0)
{
ExecuteThread eT = new ExecuteThread(entryQueue.Dequeue(), exe, gReference,
new startNextThread(startNextThread));

Thread newThread = new Thread(eT.ThreadProcess);
newThread.Start();
}

最佳答案

你应该把你评论的行 之前 调用WaitForExit .有关详细信息,请参阅 documentation 的备注部分.

此外,您应该使用 Stopwatch 类来测量操作的持续时间。见Remarks section of DateTime.Now 了解更多信息。

更新:
您可以尝试使用输出流的异步读取来查看这是否有帮助。为此,请更改您的 Parse像这样的方法:

this.filePExe.OutputDataReceived += 
(s, e) => this.pInfo.additionalMessage += e.Data + Environment.NewLine;
this.filePExe.Start();
this.filePExe.BeginOutputReadLine();
this.filePExe.WaitForExit();
this.filePExe.Close();

关于c# - 任何人都可以看到我重定向这些进程的输出的方式有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6748189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com