progressBar+线程举例(winform,c#)

本文对比了三种不同的方法来更新Windows窗体中的进度条:直接更新UI、使用委托调用和后台线程更新。每种方法都有其适用场景,直接更新适合简单任务,而线程更新则用于长时间运行的任务以避免UI冻结。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int count = 1000;  //测试总数
/// <summary>
/// 方法1
/// </summary>
private void setthread()
{
this.progressBar1.Minimum = 0;
this.progressBar1.Value = 0;
this.progressBar1.Maximum = count;
for (int i = 0; i < count; i++)
{
this.progressBar1.Value++;
Application.DoEvents();
this.label2.Text = "总数"+count.ToString()+",剩余数量:" + Convert.ToString(count-i-1);
}
}
/// <summary>
/// 方法2
/// </summary>
private void setthread2()
{
this.progressBar1.Minimum = 0;
this.progressBar1.Value = 0;
this.progressBar1.Maximum = count;
for (int i = 0; i < count; i++)
{
this.progressBar1.Value++;
this.label2.Text = "总数" + count.ToString() + ",剩余数量:" + Convert.ToString(count - i - 1);
this.label2.Refresh();
}
}
/// <summary>
/// 方法3,线程
/// </summary>
private void setthread3()
{
for (int i = 0; i < count; i++)
{
string tmptext = "总数" + count.ToString() + ",剩余数量:" + Convert.ToString(count - i - 1);
this.Invoke(new setLabel(setLabel2), tmptext);
this.Invoke(new setProgressBar(setProgressBar1),1);
}
}
private void button1_Click(object sender, EventArgs e)
{
setthread();
}
private void button2_Click(object sender, EventArgs e)
{
setthread2();
}
System.Threading.Thread thread;
private void button3_Click(object sender, EventArgs e)
{
this.progressBar1.Minimum = 0;
this.progressBar1.Value = 0;
this.progressBar1.Maximum = count;
System.Threading.Thread.CurrentThread.IsBackground = true;
thread = new System.Threading.Thread(new System.Threading.ThreadStart(setthread3));
thread.Start();
}
public delegate void setLabel(string text);
public delegate void setProgressBar(int value);
private void setLabel2(string text)
{
this.label2.Text = text;
}
private void setProgressBar1(int value)
{
this.progressBar1.Value = this.progressBar1.Value+value;
}

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

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

 
private bool flag=false;
 
private System.Threading.Thread timerThread;
 
private int a=0;

private void StopThread()
{
if (timerThread != null)
{
  timerThread.Interrupt();
  timerThread
= null;
}
}
public void ThreadProc()
{

for (int i = 0; i < 10000; i++)
{
 
for (int j = 0; j < 10000; j++) ;
  a
= a + i;
}
flag
=true;

}
private void progress_Load(object sender, System.EventArgs e)
{
this.progressBar1.Minimum = 0;
this.progressBar1.Maximum = 10000;
}
private void button1_Click(object sender, System.EventArgs e)
{
StopThread();
timerThread
= new Thread(new ThreadStart(ThreadProc));
timerThread.IsBackground
= true;
timerThread.Start();
while (flag == false)
{
 
while (this.progressBar1.Value < this.progressBar1.Maximum)
  
this.progressBar1.Value += 1;
 
if (progressBar1.Value == progressBar1.Maximum)
  
this.progressBar1.Value = 0;
}
Form1 frm1
=new Form1(a.ToString());
frm1.Show();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值