C# 程序自动化 - 程序 hangs/doesn 不工作
C# Program automation - Program hangs/doesn't work
我一直在尝试编写一个程序来自动执行 运行 在我的计算机上执行不同进程的过程。到目前为止,我得到了以下程序 运行 一个 BleachBit 的控制台版本(类似于 CCleaner),该进程出现在任务管理器中,它达到大约 25kb 进程 RAM,然后 CPU 使用率变为 0%然后坐在那里很久什么都不做,从不放弃。
我的代码中是否有什么错误可能导致这种情况发生?
我尝试编辑 app.manifest 以确保程序必须 运行 作为管理员,以防它需要更多权限
此外,当 运行在 bat 文件中将类似代码与 运行 程序关联时,它会打开自己的 windows 并且 运行 没问题,所以我不确定。在正确方向上的任何帮助都会很棒。
下面是我 运行ning 的代码。
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;
}
将这些行切换为:
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
中发现了这个死锁问题
它在代码块中指出:“// 为避免死锁,始终先读取输出流然后等待。”
我一直在尝试编写一个程序来自动执行 运行 在我的计算机上执行不同进程的过程。到目前为止,我得到了以下程序 运行 一个 BleachBit 的控制台版本(类似于 CCleaner),该进程出现在任务管理器中,它达到大约 25kb 进程 RAM,然后 CPU 使用率变为 0%然后坐在那里很久什么都不做,从不放弃。
我的代码中是否有什么错误可能导致这种情况发生? 我尝试编辑 app.manifest 以确保程序必须 运行 作为管理员,以防它需要更多权限
此外,当 运行在 bat 文件中将类似代码与 运行 程序关联时,它会打开自己的 windows 并且 运行 没问题,所以我不确定。在正确方向上的任何帮助都会很棒。
下面是我 运行ning 的代码。
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;
}
将这些行切换为:
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
中发现了这个死锁问题它在代码块中指出:“// 为避免死锁,始终先读取输出流然后等待。”