当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就很容易写了。
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class AsynCaller :IAsynCaller
{
private Delegate target = null;
private object[] theArgs = null;
private Thread backThread = null ;
private bool canceled = false ;
private CBackTaskCompleted taskCompleted;
private CBackExceptionThrown exceptionThrown;
private CBackTaskCanceled taskCanceled ;
#region IAsynCaller 成员
public void Initialize(Delegate method ,object[] args)
{
this.target = method ;
this.theArgs = args ;
}
public void Start()
{
this.backThread = new Thread(new ThreadStart(this.BackThread)) ;
this.backThread.Start() ;
}
public void Cancel()
{
this.canceled = true ;
this.backThread.Abort() ;
}
#region BackThread
private void BackThread()
{
try
{
object result = this.target.DynamicInvoke(this.theArgs) ;//动态调用
this.ActivateTaskCompleted(result) ;
}
catch(Exception ee)
{
if((this.canceled) || (ee is ThreadAbortException)) //任务被取消
{
this.ActivateTaskCanceled() ;
}
else
{
this.ActivateExceptionThrown(ee) ;
}
}
}
#endregion
#region event
private void ActivateTaskCompleted(object result)
{
if(this.taskCompleted != null)
{
this.taskCompleted(result) ;
}
}
private void ActivateExceptionThrown(Exception ee)
{
if(this.exceptionThrown != null)
{
this.exceptionThrown(ee) ;
}
}
private void ActivateTaskCanceled()
{
if(this.taskCanceled != null)
{
this.taskCanceled() ;
}
}
public event CBackTaskCompleted TaskCompleted
{
add
{
this.taskCompleted += value ;
}
remove
{
this.taskCompleted -= value ;
}
}
public event CBackTaskCanceled TaskCanceled
{
add
{
this.taskCanceled += value ;
}
remove
{
this.taskCanceled -= value ;
}
}
public event CBackExceptionThrown ExceptionThrown
{
add
{
this.exceptionThrown += value ;
}
remove
{
this.exceptionThrown -= value ;
}
}
#endregion
#endregion
}
下面给出一个示例来说明如何使用IAsynCaller。
比如在一个Form中,有两个按钮,一个用于启动异步调用,一个用于取消操作。首先写一个耗时的方法,用于异步调用:
private
void
ComputeTask(
int
count)
{
for ( int i = 0 ;i < count ;i ++ )
{
Thread.Sleep( 500 ) ;
this .label1.Text = string .Format( " 第{0}次 " ,i) ;
}
}
然后在Form中添加成员变量:
{
for ( int i = 0 ;i < count ;i ++ )
{
Thread.Sleep( 500 ) ;
this .label1.Text = string .Format( " 第{0}次 " ,i) ;
}
}
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接口事件的事件处理函数也是在后台线程中调用的,也存在同样的问题。