近几天回顾了一下多线程刷新进度条的问题。自己记录一下:
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...
-----------------------------------------------------------------------------------------------------------------------------------------