Display progress bar while doing some work in C#?

后端 未结 13 2022
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 12:07

I want to display a progress bar while doing some work, but that would hang the UI and the progress bar won\'t update.

I have a WinForm ProgressForm with a Pro

13条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 13:07

    And another example that BackgroundWorker is the right way to do it...

    using System;
    using System.ComponentModel;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace SerialSample
    {
        public partial class Form1 : Form
        {
            private BackgroundWorker _BackgroundWorker;
            private Random _Random;
    
            public Form1()
            {
                InitializeComponent();
                _ProgressBar.Style = ProgressBarStyle.Marquee;
                _ProgressBar.Visible = false;
                _Random = new Random();
    
                InitializeBackgroundWorker();
            }
    
            private void InitializeBackgroundWorker()
            {
                _BackgroundWorker = new BackgroundWorker();
                _BackgroundWorker.WorkerReportsProgress = true;
    
                _BackgroundWorker.DoWork += (sender, e) => ((MethodInvoker)e.Argument).Invoke();
                _BackgroundWorker.ProgressChanged += (sender, e) =>
                    {
                        _ProgressBar.Style = ProgressBarStyle.Continuous;
                        _ProgressBar.Value = e.ProgressPercentage;
                    };
                _BackgroundWorker.RunWorkerCompleted += (sender, e) =>
                {
                    if (_ProgressBar.Style == ProgressBarStyle.Marquee)
                    {
                        _ProgressBar.Visible = false;
                    }
                };
            }
    
            private void buttonStart_Click(object sender, EventArgs e)
            {
                _BackgroundWorker.RunWorkerAsync(new MethodInvoker(() =>
                    {
                        _ProgressBar.BeginInvoke(new MethodInvoker(() => _ProgressBar.Visible = true));
                        for (int i = 0; i < 1000; i++)
                        {
                            Thread.Sleep(10);
                            _BackgroundWorker.ReportProgress(i / 10);
                        }
                    }));
            }
        }
    }
    

提交回复
热议问题