在我的 C# 应用程序中,我定义并启动了一个这样的过程:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = "Resources/";
startInfo.FileName = "batch.exe";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.Arguments = "-h usb -o read";
Process process = new Process();
process.StartInfo = startInfo;
process.EnableRaisingEvents = true;
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.CancelOutputRead();
process.Close();
我在这个过程中使用 DataReceivedEvent 来保存它的输出:
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != String.Empty && e.Data != null)
{
//Save data
result.Add(e.Data);
if (progressbar.InvokeRequired)
progressbar.Invoke(new ThreadStart(() =>
{
progressbar.PerformStep();
}));
else
progressbar.PerformStep();
}
}
同时,每次触发 DataReceived 事件时,我都想更新一个进度条。我不知道调用进度条的代码有什么问题,但线程永远不会得到处理,更糟糕的是,当它到达代码的那部分而不抛出任何错误时应用程序锁定(在 Visual Studio 中调试时)。关于如何做到这一点的任何想法?谢谢!