单参数委托通用类 C#

 

  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 > 100break;
23                 System.Threading.Thread.Sleep(2000);
24             }
25         }
26         /// <summary>
27         /// Close the form
28         /// </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 form
37         /// </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 event
45 
46 
47 #region Update logs information
48         /// <summary>
49         /// Update logs information
50         /// </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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值