delegate的BeginInvoke和EndInvoke的小例子
2009-12-30 16:58
281 查看
《.NET本质论》上写得很清楚,建议看看这本书
下面这段代码,是个小实例,说明异步Delegate是如何使用的:
设计了两个代理,一个是有返回值的,一个是没有返回值的。
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("GetDate process is begin.");
GetDateDelegate gd = new GetDateDelegate(this.GetCurrentDate);
gd.BeginInvoke(new AsyncCallback(this.CallbackMethod),null);
}
// Invoke a complex process.
private void button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Complex process is begin.");
ComplexProcessDelegate cpd =
new ComplexProcessDelegate(this.DoComplexWork);
// cpd(); // synchornized invoke.
IAsyncResult iar = cpd.BeginInvoke(null, null); // aynchornized invoke.
// iar.AsyncWaitHandle.WaitOne(); // waiting util the process is over.
// Synchornized ...
MessageBox.Show("Complex process is end.");
}
private void DoComplexWork()
{
System.Threading.Thread.Sleep(5000);
}
private string GetCurrentDate()
{
System.Threading.Thread.Sleep(5000);
return DateTime.Now.ToString();
}
private void CallbackMethod(IAsyncResult iar)
{
AsyncResult ar = (AsyncResult) iar;
GetDateDelegate gd = (GetDateDelegate) ar.AsyncDelegate;
string msg = gd.EndInvoke(iar);
MessageBox.Show(msg);
}
public delegate void ComplexProcessDelegate();
public delegate string GetDateDelegate();
下面这段代码,是个小实例,说明异步Delegate是如何使用的:
设计了两个代理,一个是有返回值的,一个是没有返回值的。
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("GetDate process is begin.");
GetDateDelegate gd = new GetDateDelegate(this.GetCurrentDate);
gd.BeginInvoke(new AsyncCallback(this.CallbackMethod),null);
}
// Invoke a complex process.
private void button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Complex process is begin.");
ComplexProcessDelegate cpd =
new ComplexProcessDelegate(this.DoComplexWork);
// cpd(); // synchornized invoke.
IAsyncResult iar = cpd.BeginInvoke(null, null); // aynchornized invoke.
// iar.AsyncWaitHandle.WaitOne(); // waiting util the process is over.
// Synchornized ...
MessageBox.Show("Complex process is end.");
}
private void DoComplexWork()
{
System.Threading.Thread.Sleep(5000);
}
private string GetCurrentDate()
{
System.Threading.Thread.Sleep(5000);
return DateTime.Now.ToString();
}
private void CallbackMethod(IAsyncResult iar)
{
AsyncResult ar = (AsyncResult) iar;
GetDateDelegate gd = (GetDateDelegate) ar.AsyncDelegate;
string msg = gd.EndInvoke(iar);
MessageBox.Show(msg);
}
public delegate void ComplexProcessDelegate();
public delegate string GetDateDelegate();
相关文章推荐
- 通过委托来实现异步 Delegate的BeginInvoke和EndInvoke
- Delegate.BeginInvoke例子
- (delegate)BeginInvoke&EndInvoke
- delegate 中的BeginInvoke和EndInvoke方法
- Delegate.BeginInvoke例子
- C#用委托(Delegate)的BeginInvoke和EndInvoke方法操作线程
- C# this.BeginInvoke()与Delegate.BeginInvoke()的区别
- C#线程:BeginInvoke和EndInvoke方法
- Invoke and BeginInvoke BeginInvoke和EndInvoke方法 (转)
- C#线程系列讲座(1):BeginInvoke和EndInvoke方法
- Mcad学习笔记之委托再理解(delegate的构造器,MulticastDelegate,BeginInvoke,EndInvoke,Invoke4个方法的探讨)
- (转)C#线程系列讲座(1):BeginInvoke和EndInvoke方法
- C#线程系列1:BeginInvoke和EndInvoke方法
- 转:C#线程系列讲座(1) BeginInvoke和EndInvoke方法
- C#线程系列讲座(1):BeginInvoke和EndInvoke方法
- Control.BeginInvoke vs. delegate.BeginInvoke(3)转
- C# BeginInvoke和EndInvoke方法
- C#线程系列讲座(1):BeginInvoke和EndInvoke方法
- Invoke and BeginInvoke BeginInvoke和EndInvoke方法 (转)
- delegate BeginInvoke& EndInvoke