110,571
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int num = 0;
private void button1_Click(object sender, EventArgs e)
{
//只执行这一个方法更新界面
ThreadUpdate(num+=10);
}
private void ThreadUpdate(int num)
{
//新开一个后台线程,并开始执行
new Thread(delegate ()
{
UpdateBar(progressBar1, num);
UpdateLab(label1, num);
})
{ IsBackground = true }.Start();
}
private void UpdateBar(ProgressBar bar, int num)
{
new Action(() =>
{
//更新界面进度条的数值
bar.Invoke(new Action(() =>
{
if (num >= bar.Minimum && num <= bar.Maximum)
bar.Value = num;
}));
}).Invoke();
}
private void UpdateLab(Label lab, int str)
{
new Action(() =>
{
//更新界面Lab的数值
lab.Invoke(new Action(() =>
{
lab.Text = "处理进度: " + str + " %";
}));
}).Invoke();
}
}
}