同步事件
1.声明事件
//负责传递消息
public delegate void MethodCall(string message);
public static event MethodCall requestdata;
2.注册事件
Page_Load事件中注册事件
requestdata+= new MethodCall(FormDataGridViewDataTable_requestdata);
3.显式的触发事件
private void button8_Click(object sender, EventArgs e)
{
if (requestdata != null) requestdata("Click Me"+DateTime.Now.ToString());
}
4.执行事件
private void FormDataGridViewDataTable_requestdata(string message)
{
Text = message;
}
异步事件
1.声明事件
public event EventHandler AnalyseFileDataCompleted;
public void OnAnalyseFileDataCompleted()
{
EventHandler handler = (EventHandler)AnalyseFileDataCompleted;
if (handler != null)
{
handler.Invoke(this, null);
}
}
2.注册事件
AnalyseFileDataCompleted+=new EventHandler(FormDataGridViewDataTable_AnalyseFileDataCompleted);
3.显式的触发事件
private void button8_Click(object sender, EventArgs e)
{
OnAnalyseFileDataCompleted();
}
4.执行事件
private void FormDataGridViewDataTable_AnalyseFileDataCompleted(object sender,EventArgs e)
{
for (int i = 0; i < 12000000; i++)
{
Text = "异步事件";
}
MessageBox.Show("00");
}
泛型异步事件
1.声明事件
public event EventHandler<FileAnalysisEventArgs<T>> AnalyseFileDataChanged;
public void OnAnalyseFileDataChanged(FileAnalysisEventArgs<T> arg)
{
EventHandler<FileAnalysisEventArgs<T>> handler = (EventHandler<FileAnalysisEventArgs<T>>)AnalyseFileDataChanged;
if (handler != null)
{
handler.Invoke(this, arg); //使用异步传送消息
}
}
2.注册事件
_fileAnalyse.FileEvent.AnalyseFileDataCompleted += new EventHandler<FileAnalysisEventArgs<T>>(FileEvent_AnalyseFileDataCompleted);
3.显式的触发事件
private void button8_Click(object sender, EventArgs e)
{
_fileAnalyseEvent.OnAnalyseFileDataChanged(new FileAnalysisEventArgs<T> { AnalyseEntity = tmpEty, LineNum = lineNum });
}
4.执行事件
protected void FileEvent_AnalyseFileDataChanged(object sender, FileAnalysisEventArgs<T> e)
{
_form.Invoke((MethodInvoker)delegate
{
});
}