当UI动作引发一个耗时的计算时,我们经常需要将这个耗时的过程放到后台线程中去完成,然后获取该过程的结果。使用.NET提供的默认设施,无论是使用Thread还是使用异步调用,细节都比较繁琐。在前几天的blog上也看到了有些兄台的解决方案,但是觉得还不够好用,于是自己封了一个AsynCaller。
AsynCaller通过事件来通知外部异步调用的结果,IAsynCaller接口如下:
public
interface
IAsynCaller
{
// Delegate用于动态调用目标方法
void Initialize(Delegate method , object [] args) ;
void Start() ; // 启动异步操作
void Cancel() ; // 取消操作
event CBackTaskCompleted TaskCompleted ;
event CBackExceptionThrown ExceptionThrown ;
event CBackTaskCanceled TaskCanceled ;
}
public delegate void CBackTaskCompleted( object result) ;
public delegate void CBackExceptionThrown(Exception ee) ;
public delegate void CBackTaskCanceled() ;
{
// Delegate用于动态调用目标方法
void Initialize(Delegate method , object [] args) ;
void Start() ; // 启动异步操作
void Cancel() ; // 取消操作
event CBackTaskCompleted TaskCompleted ;
event CBackExceptionThrown ExceptionThrown ;
event CBackTaskCanceled TaskCanceled ;
}
public delegate void CBackTaskCompleted( object result) ;
public delegate void CBackExceptionThrown(Exception ee) ;
public delegate void CBackTaskCanceled() ;
上面的接口清晰易懂,所需要注意的就是Initialize方法,它的第一个参数是需要进行异步调用的目标方法的委托,Delegate就像一个万能的delegate,它可以匹配到任何签名的方法。
接口出来后,其实现AsynCaller就很容易写了。
下面给出一个示例来说明如何使用IAsynCaller。
比如在一个Form中,有两个按钮,一个用于启动异步调用,一个用于取消操作。首先写一个耗时的方法,用于异步调用:
private
void
ComputeTask(
int
count)
{
for ( int i = 0 ;i < count ;i ++ )
{
Thread.Sleep( 500 ) ;
this .label1.Text = string .Format( " 第{0}次 " ,i) ;
}
}
{
for ( int i = 0 ;i < count ;i ++ )
{
Thread.Sleep( 500 ) ;
this .label1.Text = string .Format( " 第{0}次 " ,i) ;
}
}
然后在Form中添加成员变量:
private
IAsynCaller theAsynCaller
=
null
;
在构造函数中,初始化theAsynCaller,并预定对应的事件:
object
[] args
=
{
30
} ;
this .theAsynCaller = new AsynCaller() ;
this .theAsynCaller.Initialize( new CBack1( this .ComputeTask) ,args) ;
this .theAsynCaller.TaskCanceled += new CBackTaskCanceled(theAsynCaller_TaskCanceled);
this .theAsynCaller.TaskCompleted += new CBackTaskCompleted(theAsynCaller_TaskCompleted);
this .theAsynCaller = new AsynCaller() ;
this .theAsynCaller.Initialize( new CBack1( this .ComputeTask) ,args) ;
this .theAsynCaller.TaskCanceled += new CBackTaskCanceled(theAsynCaller_TaskCanceled);
this .theAsynCaller.TaskCompleted += new CBackTaskCompleted(theAsynCaller_TaskCompleted);
事件处理函数如下:
private
void
button1_Click(
object
sender, System.EventArgs e)
{
this .theAsynCaller.Start() ;
}
private void button2_Click( object sender, System.EventArgs e)
{
this .theAsynCaller.Cancel() ;
}
private void theAsynCaller_TaskCanceled()
{
MessageBox.Show( " Task Canceled " ) ;
}
private void theAsynCaller_TaskCompleted( object result)
{
MessageBox.Show( " Task complete " ) ;
}
{
this .theAsynCaller.Start() ;
}
private void button2_Click( object sender, System.EventArgs e)
{
this .theAsynCaller.Cancel() ;
}
private void theAsynCaller_TaskCanceled()
{
MessageBox.Show( " Task Canceled " ) ;
}
private void theAsynCaller_TaskCompleted( object result)
{
MessageBox.Show( " Task complete " ) ;
}
为了简化,上面的示例在后台线程中调用的了UI显示,这在真正的应用中是万万不可的。同时要注意,上面的示例中,IAsynCaller接口事件的事件处理函数也是在后台线程中调用的,也存在同样的问题。