private void ReportCompletion(IAsyncResult asyncResult) { AsyncResult ar = (AsyncResult)asyncResult; DoWorkEventHandler handler = (DoWorkEventHandler)ar.AsyncDelegate; DoWorkEventArgs args = (DoWorkEventArgs)ar.AsyncState; object result = null; Exception error = null; try { handler.EndInvoke(asyncResult); result = args.Result; } catch (Exception exception) { error = exception; } if (RunWorkerCompleted != null) { InvokeDelegate(RunWorkerCompleted.GetInvocationList(), new object[] { this, new RunWorkerCompletedEventArgs(result, error, args.Cancel) }); } }
/// <summary> /// Called when [run worker completed]. /// </summary> /// <param name="ar">The ar.</param> protected virtual void OnWorkCompleted(IAsyncResult ar) { DoWorkEventHandler doWorkDelegate = (DoWorkEventHandler)((AsyncResult)ar).AsyncDelegate; RunWorkerCompleted?.Invoke(this, new RunWorkerCompletedEventArgs(ar, null, _cancelationPending)); _isBusy = false; }
public void WorkOn <T>(ICollection <T> collection, Func <int, T, object> work) { if (Worker != null && Worker.IsBusy) { return; } var len = collection.Count; //for(int i=0;i<len;i++) foreach (var(i, el) in Enumerable.Range(0, len).Zip(collection, ValueTuple.Create)) { Worker.DoWork += (o, e) => { var bv = o as BackgroundWorker; if (bv.CancellationPending) { return; } var data = work?.Invoke(i, el); if (bv.CancellationPending) { return; } bv.ReportProgress(i * 100 / len, data); } } ; Worker.ProgressChanged += (o, e) => { Progress.Bar.Value = e.ProgressPercentage; ProgressChanged?.Invoke(o, e); }; Worker.RunWorkerCompleted += (o, e) => { Progress.Close(); RunWorkerCompleted?.Invoke(o, e); }; Worker.RunWorkerAsync(100000); }
private void RunTask() { ThreadInvoker.BackInvoke(() => { try { DoWork?.Invoke(this, new DoWorkEventArgs(this)); window.Dispatcher.Invoke(() => { window.Close(); RunWorkerCompleted?.Invoke(this, new RunWorkerCompletedEventArgs(null, null, CancellationPending)); }); } catch (Exception ex) { window.Dispatcher.Invoke(() => { window.Close(); RunWorkerCompleted?.Invoke(this, new RunWorkerCompletedEventArgs(null, ex, CancellationPending)); }); } }); window.Owner = Application.Current.MainWindow; window.ShowInTaskbar = false; }
/// <summary> /// Updates the user interface once an operation has been completed and /// sets the dialog's DialogResult depending on the value /// of the <see cref="AsyncCompletedEventArgs.Cancelled"/> property.</summary> private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { Error = e.Error; } else if (!e.Cancelled) { //assign result if there was neither exception nor cancel Result = e.Result; } //update UI in case closing the dialog takes a moment Progress = 100; //set the dialog result, which closes the dialog //DialogResult = Error == null && !e.Cancelled; Cancelled = Cancelled || e.Cancelled; RunWorkerCompleted.Raise(this, EventArgs.Empty); }
/// <summary> /// Raises the System.ComponentModel.Custom.Generic.BackgroundWorker.RunWorkerCompleted event. /// </summary> /// <param name="e">A System.ComponentModel.Custom.Generic.RunWorkerCompletedEventArgs /// that contains the event data.</param> protected virtual void OnRunWorkerCompleted(RunWorkerCompletedEventArgs <TResult> e) { RunWorkerCompleted?.Invoke(this, e); }
private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { RunWorkerCompleted?.Invoke(sender, e); frmBackground.Dispose(); }
/// <summary> /// Raises the RunWorkerCompleted event. /// </summary> /// <param name="e">A <see cref="QueuedWorkerCompletedEventArgs"/> that contains event data.</param> protected virtual void OnRunWorkerCompleted(QueuedWorkerCompletedEventArgs e) { RunWorkerCompleted?.Invoke(this, e); }
public void ShowDialog(object owner = null) { DoWork?.Invoke(this, new DoWorkEventArgs(null)); RunWorkerCompleted?.Invoke(this, new RunWorkerCompletedEventArgs(null, null, false)); }
protected virtual void OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { ProgressBar1.Style = ProgressBarStyle.Blocks; ProgressBar1.Value = 100; RunWorkerCompleted?.Invoke(sender, e); }
protected virtual void OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { RunWorkerCompleted?.Invoke(sender, e); }
protected virtual void OnRunWorkerCompleted() { RunWorkerCompleted?.Invoke(this, EventArgs.Empty); }