文章目录
- 实例一:【方便理解,常用!】
- 源码:
- 运行效果:
- 实例二:【重在理解代码本身】
- 源码:
- 运行效果:
- 参考:
实例一:【方便理解,常用!】
跨线程更新进度条【事件触发(进度条值增加的方法在另一个命名空间下的类中)】
源码:
AddNum.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ProgressBarAddNum
{public class AddNum{public static int Num = 0;public delegate void UpdateProgress(int index);public static event UpdateProgress onUpdateProgress;public static void Add(){UpdateProgressMethod(Num > 100 ? 0 : Num);Num+=2; }public static void UpdateProgressMethod(int i){if (onUpdateProgress != null){onUpdateProgress.BeginInvoke(i, null, null);}}}
}
Form1.cs
using ProgressBarAddNum;
using System;
using System.Threading;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class Form1 : Form{public Form1(){InitializeComponent();AddNum.onUpdateProgress += AddNum_onUpdateProgress;//注意:此事件绑定只绑定一次,不能多次绑定}public static Thread thread;private void button1_Click(object sender, EventArgs e){AddNum.Num = 0;thread = new Thread(Process);thread.IsBackground = true;thread.Start();}public delegate void UpdateProgress(int index);private void AddNum_onUpdateProgress(int index){if (progressBar1.InvokeRequired){UpdateProgress dus = new UpdateProgress(AddNum_onUpdateProgress);this.BeginInvoke(dus, new object[] { index });//方法一//this.BeginInvoke(dus, index);//方法二//progressBar1.Invoke(dus, new object[] { index });//方法三:也可以同时更新textBox1的值【???】//progressBar1.Invoke(dus, index);//方法四:也可以同时更新textBox1的值【???】}else{progressBar1.Value = index;textBox1.Text = index.ToString();//注意:同时更新textBox1的值}}private void Process(){while (true){if (AddNum.Num <= 100){AddNum.Add();Thread.Sleep(50);}else{break;}}}private void button2_Click(object sender, EventArgs e){if (thread != null){thread.Abort();thread = null;}progressBar1.Value = 0;textBox1.Text = "0";}}
}
运行效果:
实例二:【重在理解代码本身】
C#中的事件是建立在委托的基础上,标准的事件模型应该包括以下几点:
- 声明一个用于定义事件的委托,这里用系统自带的泛型委托原型EventHandler,如:public delegate void EventHandler(object sender,TEventArgs e);这里面的TEventArgs是由我们自定义的参数类型,继承自EventArgs基类
- 事件参数的名称以EventArgs结束
- 声明事件的委托原型即EventHandler,它的返回值为void
- 声明事件的委托原型即EventHandler,它有两个参数:sender和e,sender表示事件触发者,e表示事件触发时的参数
- 事件的声明是在普通委托声明的前面加上,event关键字,如:public event EventHandler FileUploaded;
源码:
文件下载时,要实时更新进度条,这时更新进度的方法就应该由下载类在下载的同时根据实时的下载进度利用事件去同步更新进度条的值。代码如下:
using System;
using System.Threading;
using System.Windows.Forms;namespace WindowsFormsApp2
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void ShowProcess(object sender, FileUploaderEventArgs e){//定义委托Action t = () =>{progressBar1.Value = e.FileProgress;label1.Text = e.FileProgress.ToString();};//跨线程操作this.BeginInvoke(t);Thread.Sleep(100);//【注意】:触发事件后的方法要执行完毕,才会退出while中的触发事件代码}private void button1_Click_1(object sender, EventArgs e){FileUploader f1 = new FileUploader();f1.FileUploaded += ShowProcess; //绑定事件ThreadPool.QueueUserWorkItem((a) => f1.Upload());//加入线程池}}/// <summary>/// 文件下载类/// </summary>class FileUploader{public event EventHandler<FileUploaderEventArgs> FileUploaded;//定义事件public void Upload(){var e = new FileUploaderEventArgs() { FileProgress = 0 };while (e.FileProgress < 100){//DoSomething();//执行一些下载任务e.FileProgress++;FileUploaded(this, e);//触发事件//Thread.Sleep(100);//【注意】:或者注释掉上边的代码,使用这个,实际中更常用}}}/// <summary>/// 自定义参数/// </summary>class FileUploaderEventArgs : EventArgs{public int FileProgress { get; set; }}
}
运行效果:
参考:
https://www.cnblogs.com/maitian-lf/p/3654146.html