在对话框中显示进度

发布于 2024-08-13 05:15:47 字数 778 浏览 7 评论 0原文

我有一个需要很长时间的过程,我想要一个窗口来显示进度。但是,我不知道如何显示进度。

代码如下:

if (procced)
{
    // the wpf windows :
    myLectureFichierEnCour = new LectureFichierEnCour(_myTandemLTEclass);
    myLectureFichierEnCour.Show();

    bgw = new BackgroundWorker();
    bgw.DoWork += startThreadProcessDataFromFileAndPutInDataSet;
    bgw.RunWorkerCompleted += threadProcessDataFromFileAndPutInDataSetCompleted;

    bgw.RunWorkerAsync();
}

并且:

private void startThreadProcessDataFromFileAndPutInDataSet(object sender, DoWorkEventArgs e)
{
    _myTandemLTEclass.processDataFromFileAndPutInDataSet(
        _strCompositeKey,_strHourToSecondConversion,_strDateField);
}

我可以调用 _myTandemLTEclass.processProgress 来获取进度提示。

I have a process that takes a long time, and I want a window to show the progress. But, I can't figure how to display the progress.

Here's the code:

if (procced)
{
    // the wpf windows :
    myLectureFichierEnCour = new LectureFichierEnCour(_myTandemLTEclass);
    myLectureFichierEnCour.Show();

    bgw = new BackgroundWorker();
    bgw.DoWork += startThreadProcessDataFromFileAndPutInDataSet;
    bgw.RunWorkerCompleted += threadProcessDataFromFileAndPutInDataSetCompleted;

    bgw.RunWorkerAsync();
}

And:

private void startThreadProcessDataFromFileAndPutInDataSet(object sender, DoWorkEventArgs e)
{
    _myTandemLTEclass.processDataFromFileAndPutInDataSet(
        _strCompositeKey,_strHourToSecondConversion,_strDateField);
}

I can call _myTandemLTEclass.processProgress to get a hint of the progress.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2024-08-20 05:15:47

您应该处理 ProgressChanged 事件并更新用户界面中的进度栏​​。

在执行该工作的实际函数(DoWork 事件处理程序)中,您将使用指定的参数调用 BackgroundWorker 实例的 ReportProgress 方法完成的任务量。

MSDN Library 中的BackgroundWorker 示例是一段简单的代码完成这项工作的片段。

You should handle the ProgressChanged event and update the progress bar in your user interface there.

In the actual function that does the work (DoWork event handler), you'll call the ReportProgress method of the BackgroundWorker instance with an argument specifying the amount of task completed.

The BackgroundWorker example in MSDN Library is a simple code snippet that does the job.

浅沫记忆 2024-08-20 05:15:47

您的backgroundWorker线程需要处理DoWork方法和ProgressChanged

您还需要确保将 WorkerReportsProgress 标志打开为 true(默认情况下关闭)。

参见示例代码:

private void downloadButton_Click(object sender, EventArgs e)
{
    // Start the download operation in the background.
    this.backgroundWorker1.RunWorkerAsync();

    // Disable the button for the duration of the download.
    this.downloadButton.Enabled = false;

    // Once you have started the background thread you 
    // can exit the handler and the application will 
    // wait until the RunWorkerCompleted event is raised.

    // Or if you want to do something else in the main thread,
    // such as update a progress bar, you can do so in a loop 
    // while checking IsBusy to see if the background task is
    // still running.

    while (this.backgroundWorker1.IsBusy)
    {
        progressBar1.Increment(1);
        // Keep UI messages moving, so the form remains 
        // responsive during the asynchronous operation.
        Application.DoEvents();
    }
}

Your backgroundWorker thread needs to handle the DoWork method and ProgressChanged.

You also need to make sure you turn on the WorkerReportsProgress flag to true (off by default).

See example code:

private void downloadButton_Click(object sender, EventArgs e)
{
    // Start the download operation in the background.
    this.backgroundWorker1.RunWorkerAsync();

    // Disable the button for the duration of the download.
    this.downloadButton.Enabled = false;

    // Once you have started the background thread you 
    // can exit the handler and the application will 
    // wait until the RunWorkerCompleted event is raised.

    // Or if you want to do something else in the main thread,
    // such as update a progress bar, you can do so in a loop 
    // while checking IsBusy to see if the background task is
    // still running.

    while (this.backgroundWorker1.IsBusy)
    {
        progressBar1.Increment(1);
        // Keep UI messages moving, so the form remains 
        // responsive during the asynchronous operation.
        Application.DoEvents();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文