我正在逐行讀取外部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應用程序? – SWeko
代碼引發此錯誤「此BackgroundWorker指出它不報告進度。修改WorkerReportsProgress以聲明它報告進度」。 – WiXXeY