环保网站建设公司排名,汉阴网站建设,html代码大全简单,江西正东建设工程有限公司网站文章目录实例一#xff1a;【方便理解#xff0c;常用#xff01;】源码#xff1a;运行效果#xff1a;实例二#xff1a;【重在理解代码本身】源码#xff1a;运行效果#xff1a;参考#xff1a;实例一#xff1a;【方便理解#xff0c;常用#xff01;】
跨线…
文章目录实例一【方便理解常用】源码运行效果实例二【重在理解代码本身】源码运行效果参考实例一【方便理解常用】
跨线程更新进度条【事件触发(进度条值增加的方法在另一个命名空间下的类中)】
源码
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);Num2; }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和esender表示事件触发者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/// 文件下载类/// /summaryclass FileUploader{public event EventHandlerFileUploaderEventArgs FileUploaded;//定义事件public void Upload(){var e new FileUploaderEventArgs() { FileProgress 0 };while (e.FileProgress 100){//DoSomething();//执行一些下载任务e.FileProgress;FileUploaded(this, e);//触发事件//Thread.Sleep(100);//【注意】或者注释掉上边的代码使用这个实际中更常用}}}/// summary/// 自定义参数/// /summaryclass FileUploaderEventArgs : EventArgs{public int FileProgress { get; set; }}
}运行效果 参考
https://www.cnblogs.com/maitian-lf/p/3654146.html