/// <summary> /// Completed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Worker_RunWorkCompleted(object sender, RunWorkerCompletedEventArgs e) { try { if (_mfrmProgressForm != null) { _mfrmProgressForm.Close(); _mfrmProgressForm.Dispose(); _mfrmProgressForm = null; } if (_mWorker != null) { _mWorker.DoWork -= Worker_DoWork; _mWorker.RunWorkerCompleted -= Worker_RunWorkCompleted; try { if (_mWorkerThread != null && _mWorkerThread.IsAlive) { _mWorkerThread.Abort(); } } catch (Exception) { } } //In timer, When stop progress will make thread throw AbortException if (_mWorkerThread != null && _mWorkerThread.ThreadState == ThreadState.Aborted) { WorkStoped?.Invoke(new DoWorkEventArgs <T>(_mWorkArg)); } else { RunWorkCompleted?.Invoke(new RunWorkerCompletedEventArgs <T>(e.Result, e.Error, e.Cancelled)); } } catch (Exception ex) { throw ex; } }
/// <summary> /// Start AsyncWorl /// </summary> /// <param name="Para"></param> public void AsyncStart(T Para) { //if workeven is null ,express user do not regist event if (DoWork == null) { return; } _miWorkerStartDateSecond = 0; _miShowProgressCount = 0; //init if (_mWorker != null && _mWorker.IsBusy) { _mWorker.CancelAsync(); _mWorker = null; } _mWorker = new BackgroundWorker(); //create progressbar _mfrmProgressForm = new ProgressbarEx(); //add event _mWorker.DoWork += Worker_DoWork; _mWorker.RunWorkerCompleted += Worker_RunWorkCompleted; _mWorker.WorkerReportsProgress = true; _mWorker.WorkerSupportsCancellation = true; //Set Whole Para _mWorkArg = Para; _mWorker.RunWorkerAsync(Para); //Start timer StartTimer(); _mfrmProgressForm.StartPosition = FormStartPosition.CenterParent; _mfrmProgressForm.ShowDialog(); }