close
1.建立 Delegate
2.建立 Event
3.在要啟動事件的地方, 註冊事件處理常式
4.運行會觸發事件的 method
4.運行會觸發事件的 method
5.在會觸發事件的 method中,要檢查
/////////////////////////////
//建立事件,觸發他
if (GetInfoEvent != null)
{
//給予事件所註冊的method ,參數
GetInfoEvent(i, total);
}
////////////////////////////////////////
if (GetInfoEvent != null)
{
//給予事件所註冊的method ,參數
GetInfoEvent(i, total);
}
////////////////////////////////////////
using System;
using System.Windows.Forms;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void CountChangeHandler(string total);
public delegate void CountChangeHandler(string total);
// The event we publish
public event CountChangeHandler CountChangEvent;
public event CountChangeHandler CountChangEvent;
void Form1_CountChang(string total)
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine(total);
}
Console.WriteLine(total);
}
private void button1_Click(object sender, EventArgs e)
{
CountChangEvent += new CountChangeHandler(Form1_CountChang);
_1AddTo10();
}
{
CountChangEvent += new CountChangeHandler(Form1_CountChang);
_1AddTo10();
}
public int _1AddTo10()
{
int total = 0;
for (int i = 1; i <= 10; i++)
{
total = total + i;
//建立事件,觸發他
if (CountChangEvent != null)
{
//給予事件所註冊的method ,參數
CountChangEvent(total.ToString());
}
}
return total;
}
{
int total = 0;
for (int i = 1; i <= 10; i++)
{
total = total + i;
//建立事件,觸發他
if (CountChangEvent != null)
{
//給予事件所註冊的method ,參數
CountChangEvent(total.ToString());
}
}
return total;
}
}
}
全站熱搜
留言列表