C# 多线程刷新进度条

近几天回顾了一下多线程刷新进度条的问题。自己记录一下:

 

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

namespace MultyThreads
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int maxPbar = 30000;
        int nowPbar = 0;
        Thread threadObj;
        private delegate void SetPos(int nowNum);

        /// <summary>
        /// 设置界面控件方法
        /// </summary>
        /// <param name="nowNum"></param>
        private void SetPbar(int nowNum)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    SetPos sPos = new SetPos(SetPbar);
                    this.Invoke(sPos, nowNum);
                }
                else
                {
                    progressBar1.Value = nowNum * (progressBar1.Maximum) / maxPbar;
                    textBox1.Text = nowPbar.ToString();
                }
            }
            catch { }
        }

        private void btnSinge_Click(object sender, EventArgs e)
        {
            if (threadObj != null && threadObj.IsAlive)
            {
                threadObj.Abort();
            }
            threadObj = new Thread(new ThreadStart(StartXX));
            threadObj.Start();
        }
        private void StartXX()
        {
            nowPbar = 0;
            while (nowPbar < maxPbar)
            {
                nowPbar++;
                //Thread.Sleep(100);
                SetPbar(nowPbar);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (threadObj.IsAlive)
            {
                threadObj.Abort();
            }
        }

#region  Form.Designer.cs
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
            this.btnSinge = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // progressBar1
            //
            this.progressBar1.Location = new System.Drawing.Point(24, 65);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(236, 23);
            this.progressBar1.TabIndex = 0;
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(24, 20);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(236, 20);
            this.textBox1.TabIndex = 1;
            //
            // btnSinge
            //
            this.btnSinge.Location = new System.Drawing.Point(167, 106);
            this.btnSinge.Name = "btnSinge";
            this.btnSinge.Size = new System.Drawing.Size(75, 23);
            this.btnSinge.TabIndex = 3;
            this.btnSinge.Text = "开始";
            this.btnSinge.UseVisualStyleBackColor = true;
            this.btnSinge.Click += new System.EventHandler(this.btnSinge_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 153);
            this.Controls.Add(this.btnSinge);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.progressBar1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.ProgressBar progressBar1;
        private System.Windows.Forms.TextBox textBox1;
        private System.ComponentModel.BackgroundWorker backgroundWorker1;
        private System.Windows.Forms.Button btnSinge;
        #endregion

     
    }
}

 ------------------------------------------------------------------------------------------------------------------------------------

------------------------------如果更新界面,请调用主线程,代码类似于------------------------------------------

// running on worker thread...

  string newText = LongRunningOperation();

  //switch to UI thread
  this.Invoke((MethodInvoker)delegate {
    // this bit runs on the UI thread
    this.Text = newText;
  });

-----------------------------------------------------------------------------------------------------------------------------------------

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值