1

我在这个问题上尝试了很多链接,但找不到任何帮助。我有这段代码,我在其中执行一个 .exe 文件,该文件从网页检索数据并在我的列表框中显示结果。这需要时间并且窗口冻结,我决定使用 BackgoundWorker 但所有示例都使用循环来更新进度条,在我的情况下没有循环,我怎样才能使我的进度条增加?请帮我解决这个问题,这是我的代码,

        labelstat.Text = "Please wait..";
        // Start the child process.
        Process p = new Process();


        // Redirect the output stream of the child process.
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.FileName = "Out.exe";

        p.Start();
        // Do not wait for the child process to exit before
        // reading to the end of its redirected stream.
        // p.WaitForExit();
        // Read the output stream first and then wait.
        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        checkedListBox1.Items.AddRange(output.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));

我试着把这个放在

        backgroundWorker_DoWork

方法,它在后台执行我的任务,但我不知道如何根据我的代码执行的过程增加进度条?:( 非常感谢你..

4

1 回答 1

0

不确定您是否能够更改进度条,毕竟您怎么知道程序完成了多少工作百分比。你可以做的是保持 UI 响应。您可能已经使用后台工作程序完成了这项工作,但是您可以通过在程序写入输出流时向 UI 添加行来做更多事情(尽管考虑到它需要下载数据,您可能会发现直到一下子就完成了)。

您可以通过创建一个一直运行到p.HasExisted(表示程序完成)的循环来做到这一点。在该循环中,您将报告进度(BackgroundWorker.ReportProgress),可能会传递到目前为止的输出字符串数组。然后,在后台工作人员的进度更改事件处理程序中,您将使用您在报告进度时传入的数组对象更新您的 UI。

于 2012-09-26T10:29:17.000 回答