using System.ComponentModel; using System.Windows.Threading; using System.Threading; using System.Windows; namespace MefBasic.Threading { public class BackgroundWorkerEx : BackgroundWorkerWrapper, IBackgroundWorkerEx { protected override void OnProgressChanged(ProgressChangedEventArgs e) { Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new ParameterizedThreadStart(InvokeProcessChanged), e); } private void InvokeProcessChanged(object parameter) { var e = (ProgressChangedEventArgs)parameter; base.OnProgressChanged(e); } protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e) { InvokeRunWorkerCompleted(e); } private void InvokeRunWorkerCompleted(object parameter) { var e = (RunWorkerCompletedEventArgs)parameter; base.OnRunWorkerCompleted(e); IsWorkerBusy = false; } public void RunWorkerBlocked() { IsWorkerBusy = true; RunWorkerAsync(); Block(); } public void Block() { if (Application.Current == null) return; while (IsWorkerBusy) { DoEvents(); } } private static void DoEvents() { Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { })); } public void RunWorkerBlocked(object parameter) { IsWorkerBusy = true; RunWorkerAsync(parameter); if (Application.Current == null) return; while (IsWorkerBusy) DoEvents(); } } }
By viewing downloads associated with this article you agree to the Terms of use and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Math Primers for Programmers