【发布时间】:2015-11-11 07:57:17
【问题描述】:
我在后台工作人员的帮助下逐行读取外部 exe 的控制台,我将控制台的每一行分配给一个标签。问题是标签没有随控制台行更新。代码如下
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
int i = 0;
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = EXELOCATION;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = Program.path;
startInfo.RedirectStandardOutput = true;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (exeProcess = Process.Start(startInfo))
{
using (StreamReader reader = exeProcess.StandardOutput)
{
string result;
while ((result = reader.ReadLine()) != null)
{
// object param = result;
e.Result = result;
bgWorker.ReportProgress(i++);
}
}
}
}
catch
{
// Log error.
}
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label.Text = e.ToString();
label.Refresh();
}
我该如何解决这个问题
【问题讨论】:
-
你确定它没有发生吗?文件太小以至于发生得太快是偶然的吗?
-
这是 WinForms 还是 WPF 应用程序?
-
代码引发此错误“此 BackgroundWorker 声明它不报告进度。修改 WorkerReportsProgress 以声明它确实报告进度”。
-
那么异常消息是否足以让您得出结论,您必须添加 bgWorker.WorkerReportsProgress = true; ?
标签: c# .net multithreading