/// 窗体加载事件
/// </summary>
/// <param name="obj"></param>
private void Loaded(object obj)
{
_selfView = obj as UserControl;
if (_selfView == null)
{
LogHelper.WriteLog(this.GetType() + ".Loaded方法中,通过命令绑定传入的参数obj为null");
MaskPanelHelper.ShowMessageBoxWithMask("查验失败,请重试!");
return;
}
//模拟做了一个循环(没有实际意义,只是表明异步并发查验多条记录)
for(i=0;i<10;i++)
{
AsyncCheckSingleInvoice();
}
}
Action _action = null;
/// <summary>
/// 异步查验单张发票
/// </summary>
private void AsyncCheckSingleInvoice()
{
//var action = new Action(CheckSingleInvoice);
//action.BeginInvoke(CheckInvoicesCallback, null);
_action = new Action(CheckSingleInvoice);
_action.BeginInvoke(CheckInvoicesCallback, null);
}
/// <summary>
/// 查验单票(异步委托方法)
/// </summary>
private Invoice CheckSingleInvoice()
{
//网络故障检测
if (!NetWorkHelper.IsConnectedInternet())
{
Application.Current.Dispatcher.Invoke((Action)(() => { MaskPanelHelper.ShowMessageBoxWithMask("网络故障,请检查"); }));
return;
}
.......
return _singleInvoice; //查验完毕,再返回这张发票信息
}
/// <summary>
/// 查验单票完毕后的回调
/// </summary>
private void CheckInvoicesCallback(IAsyncResult ir)
{
//异步委托的结果
AsyncResult ar = ir as AsyncResult;
if (ar != null)
{
var action = (Action)ar.AsyncDelegate;//异步委托方法
action.EndInvoke(ar);//获得 "被异步委托”的“CheckSingleInvoice”方法的返回值
}
........
}
}
说明:当这个 “被异步委托” 的 “CheckSingleInvoice”执行完毕后,就会自动执行 “CheckInvoicesCallback” 回调方法,并且在CheckInvoicesCallback中可以获得CheckSingleInvoice方法的返回值