C# 线程更新UI界面
程序员文章站
2022-06-10 14:33:51
...
首先创建ProgressBar进度条控件,一个Button按钮,和一个label控件
注意, 进度条控件的Name为:pgbWrite,按钮的Name为:button1,label的Name为:lblWriteStatus
button按钮的点击事件
private void button1_Click(object sender, EventArgs e)
{
int taskCount = 10000; //任务量为10000
this.pgbWrite.Maximum = taskCount;
this.pgbWrite.Value = 0;
DataWrite dataWrite = new DataWrite(); //实例化一个写入数据的类
dataWrite.UpdateUIDelegate += UpdataUIStatus; //绑定更新任务状态的委托
dataWrite.TaskCallBack += Accomplish; //绑定完成任务要调用的委托
Thread thread = new Thread(new ParameterizedThreadStart(dataWrite.Write)); //启动线程,执行方法
thread.IsBackground = true; //后台进程
thread.Start(taskCount); //传入参数,并启动
}
声明一个更新ui界面的方法
private void UpdataUIStatus(int step)
{
if (InvokeRequired)
{
this.Invoke(new AsynUpdateUI(delegate(int s)
{
this.pgbWrite.Value += s;
this.lblWriteStatus.Text = this.pgbWrite.Value.ToString() + "/" + this.pgbWrite.Maximum.ToString();
}), step);
}
else
{
this.pgbWrite.Value += step;
this.lblWriteStatus.Text = this.pgbWrite.Value.ToString() + "/" + this.pgbWrite.Maximum.ToString();
}
}
声明一个写入数据的类
private class DataWrite
{
public delegate void UpdateUI(int step); //声明一个更新主线程的委托
public UpdateUI UpdateUIDelegate;
public delegate void AccomplishTask(); //声明一个在完成任务时通知主线程的委托
public AccomplishTask TaskCallBack;
/// <summary>
/// 示例方法
/// </summary>
/// <param name="lineCount"></param>
public void Write(object lineCount)
{
StreamWriter writeIO = new StreamWriter("city.txt", false, Encoding.GetEncoding("utf-8"));
//写入头
string head = "编号,省,市";
writeIO.Write(head);
for (int i = 0; i < (int)lineCount; i++)
{
writeIO.WriteLine(i.ToString() + ",北京,海淀");
//写入一条数据,调用更新主线程ui状态的委托
UpdateUIDelegate(1);
}
//任务完成时通知主线程作出相应的处理
TaskCallBack();
writeIO.Close();
}
}
当线程任务执行完之后调用的方法 /// <summary>
/// 完成任务时需要调用
/// </summary>
private void Accomplish()
{
//还可以进行其他的一些完任务完成之后的逻辑处理
MessageBox.Show("任务完成");
}
以下是整体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
delegate void AsynUpdateUI(int step);
private void button1_Click(object sender, EventArgs e)
{
int taskCount = 10000; //任务量为10000
this.pgbWrite.Maximum = taskCount;
this.pgbWrite.Value = 0;
DataWrite dataWrite = new DataWrite(); //实例化一个写入数据的类
dataWrite.UpdateUIDelegate += UpdataUIStatus; //绑定更新任务状态的委托
dataWrite.TaskCallBack += Accomplish; //绑定完成任务要调用的委托
Thread thread = new Thread(new ParameterizedThreadStart(dataWrite.Write)); //启动线程,执行方法
thread.IsBackground = true; //后台进程
thread.Start(taskCount); //传入参数,并启动
}
private void UpdataUIStatus(int step)
{
if (InvokeRequired)
{
this.Invoke(new AsynUpdateUI(delegate(int s)
{
this.pgbWrite.Value += s;
this.lblWriteStatus.Text = this.pgbWrite.Value.ToString() + "/" + this.pgbWrite.Maximum.ToString();
}), step);
}
else
{
this.pgbWrite.Value += step;
this.lblWriteStatus.Text = this.pgbWrite.Value.ToString() + "/" + this.pgbWrite.Maximum.ToString();
}
}
/// <summary>
/// 完成任务时需要调用
/// </summary>
private void Accomplish()
{
//还可以进行其他的一些完任务完成之后的逻辑处理
MessageBox.Show("任务完成");
}
private class DataWrite
{
public delegate void UpdateUI(int step); //声明一个更新主线程的委托
public UpdateUI UpdateUIDelegate;
public delegate void AccomplishTask(); //声明一个在完成任务时通知主线程的委托
public AccomplishTask TaskCallBack;
/// <summary>
/// 示例方法
/// </summary>
/// <param name="lineCount"></param>
public void Write(object lineCount)
{
StreamWriter writeIO = new StreamWriter("city.txt", false, Encoding.GetEncoding("utf-8"));
//写入头
string head = "编号,省,市";
writeIO.Write(head);
for (int i = 0; i < (int)lineCount; i++)
{
writeIO.WriteLine(i.ToString() + ",北京,海淀");
//写入一条数据,调用更新主线程ui状态的委托
UpdateUIDelegate(1);
}
//任务完成时通知主线程作出相应的处理
TaskCallBack();
writeIO.Close();
}
}
}
}