单参数委托通用类 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 > 100break;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

原创粉丝点击