C# 怎样使用进度条

2025-03-12 14:43:02
推荐回答(4个)
回答1:

这个实现的方法很多,可以用代理,BackgroundWorker, Task,Thread等等,但原理差不多,就是开一个新线程让其执行耗时操作,过程中异步更新UI界面。

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;

namespace AskAnswers
{
    public class ShowProgressStatus : Form
    {
        [STAThread]
        static void Main()
        {
            Application.Run(new ShowProgressStatus());
        }

        public ShowProgressStatus()
        {
            InitializeComponent();
        }

        /// 
        ///    Required method for Designer support - do not modify
        ///    the contents of this method with an editor
        /// 

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.btnProcess = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            
            //@design this.TrayHeight = 0;
            //@design this.TrayLargeIcon = false;
            //@design this.TrayAutoArrange = true;
            label1.Location = new System.Drawing.Point(32, 40);
            label1.Text = "Progress Value";
            label1.Size = new System.Drawing.Size(88, 24);
            label1.TabIndex = 2;
            
            progressBar1.Maximum = 10;
            progressBar1.Location = new System.Drawing.Point(8, 312);
            progressBar1.Minimum = 0;
            progressBar1.TabIndex = 0;
            progressBar1.Value = 0;
    
            //We have calculated the excat size which will result in only 20 boxes to be drawn
            
            progressBar1.Size = new System.Drawing.Size(520, 40);
            progressBar1.Step = 1;
            
            btnProcess.Location = new System.Drawing.Point(152, 168);
            btnProcess.Size = new System.Drawing.Size(144, 48);
            btnProcess.TabIndex = 1;
            btnProcess.Text = "Process";
            btnProcess.Click += new System.EventHandler(btnProcess_Click);
            
            textBox1.Location = new System.Drawing.Point(136, 40);
            textBox1.Text = "0";
            textBox1.TabIndex = 3;
            textBox1.Size = new System.Drawing.Size(184, 20);

            this.Text = "Display Progress Status";
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(616, 393);
            this.StartPosition = FormStartPosition.CenterScreen;
            
            this.Controls.Add(textBox1);
            this.Controls.Add(label1);
            this.Controls.Add(btnProcess);
            this.Controls.Add(progressBar1);
        }

        void btnProcess_Click(object sender, EventArgs e)
        {
            if (isProcessRunning)
            {
                MessageBox.Show("A process is already running.");
                return;
            }

            Task.Factory.StartNew(() => {
                   isProcessRunning = true;
                   return Directory.GetFiles(@"C:\Windows\system", "*");
                })
                .ContinueWith(files => {
                    string[] filesResult = files.Result;
                    progressBar1.Maximum = filesResult.Length;
                    Console.WriteLine("The Maximum of Progress Bar " + progressBar1.Maximum);
                    return filesResult;
                })
                .ContinueWith(files => {
                    string[] filesResult = files.Result;
                    Console.WriteLine("The files count " + filesResult.Length);
                    for (int n = 0; n < filesResult.Length; n++ )
                    {
                        Thread.Sleep(100);
                        Console.WriteLine(filesResult[n]);
                        progressBar1.Value = n + 1;
                    }
                })
                .ContinueWith(files => {
                    MessageBox.Show("Thread completed!");
                    progressBar1.BeginInvoke(
                            new Action(() =>
                            {
                                progressBar1.Value = 0;
                            }
                    ));
                    isProcessRunning = false;
                });
                // .Wait();

            // string[] files = Directory.GetFiles(@"c:\", "*");
            // var filesCount = files.Length;

            // progressBar1.Maximum = filesCount;

            // Thread.Sleep(50);

            // Thread backgroundThread = new Thread(
            //     new ThreadStart(() =>
            //     {
            //         isProcessRunning = true;

            //         for (int n = 0; n < filesCount; n++ )
            //         {
            //             Thread.Sleep(100);
            //             Console.WriteLine(files[n]);
            //             progressBar1.BeginInvoke(
            //                 new Action(() =>
            //                     {
            //                         progressBar1.Value = n;
            //                     }
            //             ));
            //         }

            //         MessageBox.Show("Thread completed!");
            //         progressBar1.BeginInvoke(
            //                 new Action(() =>
            //                 {
            //                     progressBar1.Value = 0;
            //                 }
            //         ));

            //         isProcessRunning = false;
            //     }
            // ));
            // backgroundThread.Start();

        }

        private System.Windows.Forms.Button btnProcess;
        private System.ComponentModel.Container components;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ProgressBar progressBar1;
        private bool isProcessRunning;
    }
}

回答2:

这不太好整吧...
如果非要做,你还得先查一下需要删除多少条记录,然后还得监控已删除多少条记录,频繁访问数据库不值当的...
不如就做一个ProgressBar,把它的Style属性改为Marquee,做一个“等待”的进度条就可以了。

回答3:

1.把进度条控件从工具箱中拖出
2.用线程启动查询方法
如:
progressBar.Value = 0;
progressBar.Visible = true;
new System.Threading.Thread(delegate()
{
while (progressBar.Value + 10 < progressBar.Maximum)
{
this.Invoke(func, progressBar.Value + 10);
System.Threading.Thread.Sleep(100);
}
//下载完结
{

this.Invoke(downloadOverFunc);
}

}).Start();
3.在查询方法中添加进度条事件
4,当查询方法返回值时出发进度条事件

回答4:

用backgroundworkder