日期:2014-05-18 浏览次数:21129 次
delegate\event的方法: 类X: using System; using System.Collections.Generic; using System.Windows.Forms; using System.Text; namespace WindowsFormsApplication1 { public class ClassX { public delegate void MessageDelegate(string strMsg); public event MessageDelegate MessageEvent; public void Test() { if (MessageEvent != null) { MessageEvent("类X for循环开始."); } for (int i = 0; i < 100000;i++ ) { Application.DoEvents(); } if (MessageEvent != null) { MessageEvent("类X for循环结束."); } } } } Form: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private ClassX cls = new ClassX(); private void Form1_Load(object sender, EventArgs e) { cls.MessageEvent += MessageFunction; } private void MessageFunction(string strMsg) { MessageBox.Show(strMsg); } private void button1_Click(object sender, EventArgs e) { cls.Test(); } } }