我一直在努力制作一个程序来自动化在我的计算机上运行不同进程的过程。到目前为止,我已经有了下面的程序运行控制台版本的BleachBit(类似于CCleaner),进程出现在任务管理器中,它会访问大约25 to的进程RAM,然后CPU使用率达到0%,只是坐在那里什么都不做,而且永远不会退出。
我的代码中有什么错误会导致这种情况发生吗?我尝试过编辑app.manifest,以确保程序必须以管理员身份运行,以防它需要更多的权限
另外,当在bat文件中运行类似的代码来运行程序时,它会打开自己的窗口并运行良好,所以我不确定。在正确的方向上提供任何帮助都是很棒的。
我正在运行的代码如下。
static void Main(string[] args)
{
string Log = "";
if (File.Exists(Environment.CurrentDirectory + "\\BleachBit\\bleachbit_console.exe"))
{
Log += "File exists";
Log += RunProgramCapturingOutput("\\BleachBit\\bleachbit_console.exe", "--preset --clean");
}
else
Log += "Program not found. Please place at \\BleachBit\\bleachbit_console.exe";
File.WriteAllText("log.txt", Log);
Console.ReadLine();
}
public static string RunProgramCapturingOutput(string filename, string arguments)
{
ProcessStartInfo processInfo = new ProcessStartInfo()
{
FileName = Environment.CurrentDirectory + filename,
Arguments = arguments,
CreateNoWindow = false,
UseShellExecute = false,
WorkingDirectory = Path.GetDirectoryName(Environment.CurrentDirectory + filename),
RedirectStandardError = false,
RedirectStandardOutput = true
};
Process process = Process.Start(processInfo);
process.WaitForExit();
string output = output = process.StandardOutput.ReadToEnd();
Console.WriteLine("Output: " + output);
process.Close();
return output;
}发布于 2015-06-11 05:38:44
将这些线路切换为:
string output = output = process.StandardOutput.ReadToEnd(); process.WaitForExit();
允许避免死锁。由于硬盘I/O,这个程序似乎是一个相对较慢的运行程序,只要给它一些时间,你就会看到它完成了。
我从https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx中发现了这个死锁问题
它在代码块中声明:"//为了避免死锁,始终先读取输出流,然后等待。“
https://stackoverflow.com/questions/30771983
复制相似问题