/// <summary> /// 线程执行方法,不锁定事件 /// </summary> /// <param name="method">线程方法</param> /// <param name="control">需要遮罩的控件或窗体</param> /// <param name="showError">是否显示错误信息</param> /// <returns></returns> protected bool ThreadExcute(ThreadExcuteMethod method, Control control = null, bool showError = true) { if (control == null) { control = this; } //线程异常信息 Exception threadException = null; var tl = TranslucentHelper.GenerateAndShowTranslucentLayer(control, 127, true); //使用变量同步 var isEnd = false; //定义线程,使用匿名Lambda匿名委托 var thread = new Thread(() => { try { //调用方法 method(); } catch (Exception ex) { //将线程异常信息放至父线程 threadException = ex; } finally { //设置同步变量为终止状态 isEnd = true; } }); //启动线程 thread.Start(); thread.IsBackground = true; //主线程须等待子线程执行完毕后才能继续执行 while (!isEnd) { Application.DoEvents(); } tl.HideTranslucentLayer(); if (threadException != null) { if (showError) { MsgBox.ShowError(threadException); } threadException = null; return(false); } return(true); }
/// <summary> /// 线程执行方法,不等待线程结束,线程结束后以非线程方式执行后续方法 /// </summary> /// <param name="method">线程方法</param> /// <param name="nextMethod">后续非线程方法</param> /// <param name="showWaitLayer">是否显示等待层</param> /// <param name="control">需要遮罩的控件或窗体</param> /// <returns></returns> protected void ThreadExcuteNoLock(ThreadExcuteMethod method, ThreadExcuteMethod nextMethod = null, bool showWaitLayer = false, Control control = null) { TranslucentHelper tl = null; if (showWaitLayer) { if (control == null) { control = this; } tl = TranslucentHelper.GenerateAndShowTranslucentLayer(control, 127, true); } //定义线程,使用匿名Lambda匿名委托 var thread = new Thread(() => { try { //调用方法 method(); } catch (Exception ex) { //将线程异常信息放至父线程 MsgBox.ShowError(ex); } finally { if (showWaitLayer && tl != null) { var invoker = new MethodInvoker(tl.HideTranslucentLayer); BeginInvoke(invoker); } if (nextMethod != null) { var invorker = new MethodInvoker(nextMethod); BeginInvoke(invorker); } } }); //启动线程 thread.Start(); thread.IsBackground = true; }