gpt4 book ai didi

c# - 作为 .NET 进程运行时 Git diff 卡住

转载 作者:太空宇宙 更新时间:2023-11-03 13:08:34 27 4
gpt4 key购买 nike

运行 Git diff 会卡住,直到以 System.Diagnostics.Process 运行时终止。

代码:

class Program
{
static void Main(string[] args)
{
ProcessStartInfo pInfo = new ProcessStartInfo();
pInfo.FileName = "git.exe";
pInfo.Arguments = "diff --name-only --exit-code V2.4-Beta-01 HEAD";
pInfo.WorkingDirectory = @"C:\Git";
pInfo.UseShellExecute = false;
pInfo.CreateNoWindow = true;
pInfo.RedirectStandardError = true;
pInfo.RedirectStandardOutput = true;

Process p = new Process();
p.StartInfo = pInfo;

p.Start();

p.WaitForExit(10000);

if (!p.HasExited)
{
p.Kill();
Console.WriteLine("Killed!!!");
}

Console.WriteLine(p.StandardOutput.ReadToEnd());
Console.WriteLine(p.StandardError.ReadToEnd());
Console.ReadLine();
}
}

如何避免这种情况,使程序在不超时的情况下正常存在?

最佳答案

问题是有人必须消耗 stdout 缓冲区,否则它会被填满并且进程会卡住(参见解释 here)。我尝试的差异检索了 983 行,这导致了缓冲区溢出。

以下是我的问题的解决方案:

class Program
{
static void Main(string[] args)
{
ProcessStartInfo pInfo = new ProcessStartInfo();
pInfo.FileName = "git.exe";
pInfo.Arguments = "diff --name-only --exit-code V2.4-Beta-01 HEAD";
pInfo.WorkingDirectory = @"C:\Git";
pInfo.UseShellExecute = false;
pInfo.CreateNoWindow = true;
pInfo.RedirectStandardError = true;
pInfo.RedirectStandardOutput = true;

string output = string.Empty;

Process p = new Process();
p.StartInfo = pInfo;

p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
output += e.Data + Environment.NewLine;
}
});

p.Start();

p.BeginOutputReadLine();

p.WaitForExit();
p.Close();

Console.WriteLine(output);
Console.ReadLine();
}
}

关于c# - 作为 .NET 进程运行时 Git diff 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29972789/

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