This is a mirror of official site: http://jasper-net.blogspot.com/

BackgroundWorker Helper using Lambda Expressions

| Sunday, June 10, 2012
Introduction

I have found that using Lambda Expressions with the BackgroundWorker makes maintenance much easier since you can have everything together in a single method. You can implement the BackgroundWorker using Lambdas without any sort of helper class very easily:

using (var backgroundWorker = new BackgroundWorker())
{
    Debug.Print(string.Format("Start BackgroundWorker {0}", 
         sw.ElapsedMilliseconds));
    backgroundWorker.DoWork += (s, e) =>
        {
          var baskets = FoxDataAccess.GetOrderBaskets().
            Select(i => new StaggedBlotterOrderBasketViewModel(i));
          var mainList = new ObservableCollection
            <StaggedBlotterOrderBasketViewModel>(baskets);
          e.Result = mainList;
        };
    backgroundWorker.RunWorkerCompleted += (s, e) =>
        {
          Debug.Print(string.Format("Completed BackgroundWorker {0}", 
            sw.ElapsedMilliseconds));
          _mainList = (ObservableCollection
            <StaggedBlotterOrderBasketViewModel>)e.Result;
          RaisePropertyChanged("MainListSource");
          Debug.Print(string.Format("Converted Basket Data {0}", 
            sw.ElapsedMilliseconds));
          IsBusy = false;
        };
    backgroundWorker.RunWorkerAsync();

However, using a helper can slightly reduce the code and do exactly the same thing:

Read more: Codeproject
QR: Inline image 1

Posted via email from Jasper-net

0 comments: