C#Winform的进度和BackgroundWorker的 [英] C# Winform ProgressBar and BackgroundWorker

查看:117
本文介绍了C#Winform的进度和BackgroundWorker的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个问题:

我有一个名为MainForm的形式。我有很长的操作中发生的这种形式。

I have a Form named MainForm. I have a long operation to be taken place on this form.

虽然这长时间的操作是怎么回事,我需要从对MainForm的顶部命名ProgressForm显示另一个。

While this long operation is going on, I need to show another from named ProgressForm on top of the MainForm.

ProgressForm包含一个进度条。这就需要而长操作正在进行更新。

ProgressForm contains a progress bar. Which needs to be updated while the long operation is taking place.

龙操作完成后,将ProgressForm应该自动关闭。

After the Long operation is completed, the ProgressForm should be closed automatically.

我已经写了一些code这样的:

I have written some code like the following:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ClassLibrary
{
    public class MyClass
    {
        public static string LongOperation()
        {
            Thread.Sleep(new TimeSpan(0,0,30));

            return "HelloWorld";
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BackgroungWorker__HelloWorld
{
    public partial class ProgressForm : Form
    {
        public ProgressForm()
        {
            InitializeComponent();
        }

        public ProgressBar ProgressBar
        {
            get { return this.progressBar1; }
            set { this.progressBar1 = value; }
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ClassLibrary;

namespace BackgroungWorker__HelloWorld
{
    public partial class MainForm : Form
    {
        ProgressForm f = new ProgressForm();

        public MainForm()
        {
            InitializeComponent();  
        }

        int count = 0;
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (f != null)
            {
                f.ProgressBar.Value = e.ProgressPercentage;
            }

            ++count;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled) 
            {  
                MessageBox.Show("The task has been cancelled");  
            }  
            else if (e.Error != null)  
            {                  
                MessageBox.Show("Error. Details: " + (e.Error as Exception).ToString());  
            }  
            else 
            {  
                MessageBox.Show("The task has been completed. Results: " + e.Result.ToString());  
            }
        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            if (f == null)
            {
                f = new ProgressForm();
            }

            f.ShowDialog();

            //backgroundWorker1.ReportProgress(100);

            MyClass.LongOperation();

            f.Close();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            backgroundWorker1.CancelAsync();

            this.Close();
        }
    }
}

我没有找到更新进度条的方式。

I am not finding the way to update the progressBar.

我应该在哪里放置 backgroundWorker1.ReportProgress(),我应该怎么称呼呢?

Where should I place backgroundWorker1.ReportProgress() and how should I call this?

我不能做出MyClass的任何变化。堂妹,我不知道会发生什么,或将需要多长时间才能完成这一层我的应用程序的运行。

I must not make any change in MyClass. Coz, I don't know what would happen or how long would it take to complete the operation in this layer of my application.

谁能帮我?

推荐答案

一个问题是,你在睡觉,持续30秒。通常你会长时间运行的任务中调用 ReportProgress 在不同的点。因此,为了证明这一点,你可能想改变你的code睡了1秒,但30倍 - 调用 ReportProgress 每次完成一个睡眠时间

One problem is that you're sleeping for 30 seconds. Normally you'd call ReportProgress at various points within your long-running task. So to demonstrate this, you might want to change your code to sleep for 1 second, but 30 times - calling ReportProgress each time it finishes a sleep.

另一个问题是,你显示你的 ProgressForm 的后台线程。你应该在 UI 的线程启动它,但挂钩的背景工人的 ProgressChanged 事件给它。然后,当后台工作进度报告,进度表将被更新。

Another problem is that you're showing your ProgressForm from the background thread. You should start it in the UI thread, but hook the background worker's ProgressChanged event to it. Then when the background worker reports progress, the progress form will be updated.

这篇关于C#Winform的进度和BackgroundWorker的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆