单参数委托通用类 C#
来源:互联网 发布:金十网络 编辑:程序博客网 时间:2024/06/06 04:40
1 using System;
2
3 using System.Collections.Generic;
4 using System.Text;
5 using System.Windows.Forms;
6
7 namespace DawnXZ.Tools
8 {
9 /// <summary>
10 /// General commissioned a single parameter
11 /// </summary>
12 public class DelegateTsit
13 {
14 #region Delegate & Event
15 /// <summary>
16 /// Declare a delegate
17 /// </summary>
18 /// <param name="sender">The delegate object</param>
19 /// <param name="e">The delegate event</param>
20 public delegate void UpdateInfoEventHandler(object sender, UpdateInfoEventArgs e);
21 /// <summary>
22 /// Declare a delegate event
23 /// </summary>
24 private event UpdateInfoEventHandler UpdateInfo;
25 #endregion Delegate & Event
26
27 #region Delegate event class
28 /// <summary>
29 /// Delegate event class
30 /// </summary>
31 public class UpdateInfoEventArgs : EventArgs
32 {
33 /// <summary>
34 /// To update the information
35 /// </summary>
36 public readonly string _strInfo;
37 /// <summary>
38 /// System.Windows.Forms
39 /// </summary>
40 public readonly Form _form;
41 /// <summary>
42 /// Constructed function
43 /// </summary>
44 /// <param name="form">System.Windows.Forms</param>
45 /// <param name="strInfo">To update the information</param>
46 public UpdateInfoEventArgs(Form form, string strInfo)
47 {
48 this._strInfo = strInfo;
49 this._form = form;
50 }
51 }
52 #endregion Delegate event class
53
54 #region Register & UnRegister
55 /// <summary>
56 /// To register more than one method for an event
57 /// </summary>
58 /// <param name="method">Method name</param>
59 public void Register(UpdateInfoEventHandler method)
60 {
61 UpdateInfo += method;
62 }
63 /// <summary>
64 /// Registered with the event a unique method
65 /// </summary>
66 /// <param name="method">Method name</param>
67 public void RegisterOnly(UpdateInfoEventHandler method)
68 {
69 UpdateInfo = method;
70 }
71 /// <summary>
72 /// Registered with the event against method
73 /// </summary>
74 /// <param name="method">Method name</param>
75 public void UnRegister(UpdateInfoEventHandler method)
76 {
77 UpdateInfo -= method;
78 }
79 #endregion Register & UnRegister
80
81 #region Virtual
82 /// <summary>
83 /// Can be overridden execute method
84 /// </summary>
85 /// <param name="form">System.Windows.Forms</param>
86 /// <param name="e">The delegate event</param>
87 protected virtual void OnUpdateInfo(Form form, UpdateInfoEventArgs e)
88 {
89 if (UpdateInfo != null) form.Invoke(UpdateInfo, this, e);
90 }
91 #endregion Virtual
92
93 #region Member method
94 /// <summary>
95 /// Executes a delegate method
96 /// </summary>
97 /// <param name="form">System.Windows.Forms</param>
98 /// <param name="strInfo">To update the information</param>
99 public void Executes(Form form, string strInfo)
100 {
101 UpdateInfoEventArgs e = new UpdateInfoEventArgs(form, strInfo);
102 OnUpdateInfo(form, e);
103 e = null;
104 }
105 #endregion Member method
106 }
107 }
以下代码为 From 窗体中的:
1 #region Form event 2 /// <summary> 3 /// Form Loading 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void frmMain_Load(object sender, EventArgs e) 8 { 9 System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(IamTest));10 thread.Start();11 thread.IsBackground = true;12 }13 private void IamTest()14 {15 int i = 0;16 while (true)17 {18 i++;19 DawnXZ.Tools.DelegateTsit delUpdate = new DawnXZ.Tools.DelegateTsit();20 delUpdate.Register(UpdateTxtInfoMsg);21 delUpdate.Executes(this, "I am a test information.");22 if (i > 100) break;23 System.Threading.Thread.Sleep(2000);24 }25 }26 /// <summary>27 /// Close the form28 /// </summary>29 /// <param name="sender"></param>30 /// <param name="e"></param>31 private void frmMain_Closing(object sender, CancelEventArgs e)32 {33 34 }35 /// <summary>36 /// Closed form37 /// </summary>38 /// <param name="sender"></param>39 /// <param name="e"></param>40 private void frmMain_Closed(object sender, EventArgs e)41 {42 43 }44 #endregion Form event45 46 47 #region Update logs information48 /// <summary>49 /// Update logs information50 /// </summary>51 /// <param name="sender"></param>52 /// <param name="e"></param>53 private void UpdateTxtInfoMsg(object sender, DawnXZ.Tools.DelegateTsit.UpdateInfoEventArgs e)54 {55 //this._logger.Write(strInfo);56 if (txtInfoMsg.Text.Length > 65535)57 {58 txtInfoMsg.Text = string.Empty;59 }60 txtInfoMsg.Text = string.Format("{0} {1}\r\n{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), e._strInfo, txtInfoMsg.Text);61 }62 #endregion Update logs information
- 单参数委托通用类 C#
- 单参数委托通用类 C#
- C#委托-委托作为方法的参数
- C#线程委托里带参数
- C# 委托参数方法实例
- 委托类 单例
- C#中的Array类使用泛型委托做参数
- C#委托的参数类型抗变
- C# 让方法成为参数 委托
- C# 自定义参数的 事件委托
- C#之委托(函数参数传递)
- c# 多线程委托带参数传递
- C#线程,委托的方法带参数
- C# 中的委托类
- C#通用DBHelper类
- C#网络程序设计1-2:委托实现单线程
- 分享通用基类库-C#通用缓存类
- c# 委托 envent 工具类
- Open Inventor练习-SoSelection三维场景中查找功能
- 递归合并排序
- 好书推荐--《浪潮之巅》
- 名人的读书方法
- 好书推荐--《人生不设限》
- 单参数委托通用类 C#
- C++ class中的静态 static 成员
- debian_amd64作服务器,环境配置
- $ is not a function 已解决
- 线性规划与网络流24题 10餐巾计划问题
- 解说Win32的窗口子类化
- 温度传感器(18b20测温然后在数码管上显示)
- 好书推荐--《知性力》
- Makefile笔记