TPLing Background worker in 10 min

1 minute read

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace WpfApplication1
  9. {
  10.     public class TBackgroundWorker<TResult, TProgress>
  11.     {
  12.  
  13.         public static TBackgroundWorker<TResult, TProgress> New()
  14.         {
  15.             return new TBackgroundWorker<TResult, TProgress>();
  16.         }
  17.  
  18.         public TBackgroundWorker<TResult, TProgress> StartWith(Func<object, CancellationToken, TResult> work)
  19.         {
  20.             Start = work;
  21.             return this;
  22.         }
  23.  
  24.         public TBackgroundWorker<TResult, TProgress> FinishWith(Action<TResult,object> finish)
  25.         {
  26.             Finish = finish;
  27.             return this;
  28.         }
  29.  
  30.         private readonly CancellationTokenSource _cancelationTokenSource;
  31.         private CancellationToken _cancellationToken;
  32.         private readonly TaskScheduler _uiScheduler;
  33.  
  34.  
  35.         public TBackgroundWorker()
  36.         {
  37.             _cancelationTokenSource = new CancellationTokenSource();
  38.             _uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
  39.         }
  40.  
  41.         protected Func<object, CancellationToken,TResult> Start { get; set; }
  42.         protected Action<TResult, object> Finish { get; set; }
  43.  
  44.         public void ReportProgress(Action<TProgress> progressReport,TProgress progress)
  45.         {
  46.             Task.Factory.StartNew(()=>progressReport(progress),CancellationToken.None,TaskCreationOptions.None,_uiScheduler);
  47.         }
  48.  
  49.         public TBackgroundWorker<TResult, TProgress> Begin(object state = null)
  50.         {
  51.             _cancellationToken = _cancelationTokenSource.Token;
  52.             Task<TResult>.Factory.
  53.                 StartNew(()=>Start(state,_cancellationToken),_cancellationToken).
  54.                 ContinueWith(x =>
  55.                                  Finish(x.Result,state),
  56.                                  _uiScheduler);
  57.  
  58.             return this;
  59.         }
  60.  
  61.         public void Cancel()
  62.         {
  63.             _cancelationTokenSource.Cancel(true);
  64.         }
  65.     }
  66. }


usage

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. namespace WpfApplication1
  17. {
  18.     /// <summary>
  19.     /// Interaction logic for MainWindow.xaml
  20.     /// </summary>
  21.     public partial class MainWindow : Window
  22.     {
  23.         
  24.         private TBackgroundWorker<string, int> _bg;
  25.  
  26.         public MainWindow()
  27.         {
  28.             InitializeComponent();
  29.         }
  30.  
  31.         private void a_Click(object sender, RoutedEventArgs e)
  32.         {
  33.             _bg = TBackgroundWorker<string, int>.New().
  34.                 StartWith(StartSomething).
  35.                 FinishWith(ShowResult).
  36.                 Begin();
  37.          }
  38.  
  39.         private void ShowResult(string obj,object state)
  40.         {
  41.             a.Content = obj;
  42.         }
  43.  
  44.         private string StartSomething(object arg, CancellationToken cancellationToken)
  45.         {
  46.             int i = 0;
  47.             while (i < 200)
  48.             {
  49.                 if (cancellationToken.IsCancellationRequested)
  50.                 {
  51.                     return "Cacncelled";
  52.                     
  53.                 }
  54.                 i++;
  55.                 Thread.Sleep(50);
  56.                 _bg.ReportProgress(Report,i);
  57.             }
  58.  
  59.             return "Done";
  60.         }
  61.  
  62.         private void Report(int obj)
  63.         {
  64.             a.Content = obj;
  65.         }
  66.  
  67.         private void Button_Click(object sender, RoutedEventArgs e)
  68.         {
  69.             _bg.Cancel();
  70.         }
  71.  
  72.         
  73.     }
  74. }

Tags: ,

Updated:

Comments