/// <summary> /// 回调方法 /// </summary> private static void Callback(IAsyncResult ar) { try { if (Cancelled) { return; } //若任务取消就不必EndInvoke了 MethodInfo endInvoke = ar.AsyncState.GetType().GetMethod("EndInvoke"); object[] parmsEndInvoke = new object[endInvoke.GetParameters().Length]; if (parmsEndInvoke.Length != 1)//若方法存在ref或out参数,赋值给endInvoke参数 { int i = 0; foreach (ParameterInfo p in _prmsMethod) { if (p.ParameterType.IsByRef) { parmsEndInvoke[i++] = _prmsInput[p.Position]; } } } parmsEndInvoke[parmsEndInvoke.Length - 1] = ar; _result = endInvoke.Invoke(ar.AsyncState, parmsEndInvoke); if (parmsEndInvoke.Length != 1)//从endInvoke参数取出值返给输入参数 { int i = 0; foreach (ParameterInfo p in _prmsMethod) { if (p.ParameterType.IsByRef) { _prmsInput[p.Position] = parmsEndInvoke[i++]; } } } } catch (TargetInvocationException ex) { _exception = ex.InnerException; } catch (Exception ex) { _exception = ex; } finally { _isCompleted = true; Thread.Sleep(300);//既然wf已显示,就让它正常显示一下,避免快闪 Post(arg => { if (_waitForm != null) { _waitForm.Close(); } }, (object)null); } }
protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e) { _waitForm.Close(); base.OnRunWorkerCompleted(e); }