Guest User

C# backgroundworker: Send new data object on each ReportProgress

a guest
Feb 4th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.ComponentModel;
  15. using System.Collections.ObjectModel;
  16.  
  17. namespace WpfLongThread
  18. {  
  19.     public partial class MainWindow : Window
  20.     {
  21.         ObservableCollection<Currency> currencies = new ObservableCollection<Currency>();
  22.         public ObservableCollection<Currency> Currencies { get { return currencies; } }
  23.  
  24.         private BackgroundWorker bw = new BackgroundWorker();
  25.  
  26.         public MainWindow()
  27.         {
  28.             currencies = new ObservableCollection<Currency>();
  29.             currencies.Add(new Currency { Name = "TST", Rate = 987.654321M, Description = "Initial dummy testdata" });
  30.             InitializeComponent();
  31.             bw.WorkerReportsProgress = true;
  32.             bw.WorkerSupportsCancellation = true;
  33.             bw.DoWork += new DoWorkEventHandler(bw_DoWork);
  34.             bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
  35.             bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
  36.         }
  37.         private void Thread_Click(object sender, RoutedEventArgs e)
  38.         {
  39.             if (bw.IsBusy == true)
  40.             {
  41.                 this.statusText.Text = "Already running: Please wait";
  42.                 return;
  43.             }
  44.             while (currencies.Count > 0) {
  45.                 currencies.RemoveAt(0);
  46.             }
  47.             bw.RunWorkerAsync();
  48.            
  49.         }
  50.         private void Cancel_Click(object sender, RoutedEventArgs e)
  51.         {
  52.             if (bw.WorkerSupportsCancellation == true && bw.IsBusy == true)
  53.             {
  54.                 bw.CancelAsync();
  55.                 return;
  56.             }
  57.             this.statusText.Text = " ";
  58.         }
  59.         private void bw_DoWork(object sender, DoWorkEventArgs e)
  60.         {
  61.             BackgroundWorker worker = sender as BackgroundWorker;
  62.             System.Threading.Thread.Sleep(200);
  63.             worker.ReportProgress(-5, "initializing... ...");
  64.             System.Threading.Thread.Sleep(700);
  65.             worker.ReportProgress(-1, "running ... ...");
  66.            
  67.             for (decimal i = 1; (i <= 20); i++)
  68.             {
  69.                 if ((worker.CancellationPending == true))
  70.                 {
  71.                     e.Cancel = true;
  72.                     break;
  73.                 }
  74.                 else
  75.                 {
  76.                     // Perform a time consuming operation and report progress.
  77.                     System.Threading.Thread.Sleep(700);
  78.                     string no2 = i.ToString("00");
  79.                     Currency tempCurr = new Currency() { Name = "C" + no2, Rate = 10+5*i-(i/7) , Description = "Description no " + no2 };
  80.                     worker.ReportProgress((int)(i * 5), tempCurr);
  81.                 }
  82.             }
  83.         }
  84.         private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
  85.         {
  86.             if (e.ProgressPercentage < 0)
  87.             {
  88.                 this.statusText.Text = e.UserState.ToString();
  89.                 return;
  90.             }
  91.  
  92.             this.statusText.Text = e.ProgressPercentage.ToString() + "% ";
  93.  
  94.             if (e.UserState.GetType() == typeof(WpfLongThread.Currency))
  95.             {
  96.                 currencies.Add((Currency)(e.UserState));
  97.             }
  98.         }
  99.  
  100.         private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  101.         {
  102.             if ((e.Cancelled == true))
  103.             {
  104.                 this.statusText.Text = "Canceled!";
  105.             }
  106.  
  107.             else if (!(e.Error == null))
  108.             {
  109.                 this.statusText.Text = ("Error: " + e.Error.Message);
  110.             }
  111.  
  112.             else
  113.             {
  114.                 this.statusText.Text = "Done!";
  115.             }
  116.         }
  117.         private void AddRow_Click(object sender, RoutedEventArgs e)
  118.         {
  119.             this.statusText.Text = " ";
  120.             currencies.Add(new Currency { Name = "ABC", Rate = 123.456789M, Description = "From Add button at " + DateTime.Now });
  121.         }
  122.     }
  123.  
  124.     public sealed class Currency
  125.     {
  126.         public string Name { get; set; }
  127.         public string Description { get; set; }
  128.         public decimal Rate { get; set; }
  129.  
  130.         public string NameAndDescription
  131.         {
  132.             get { return Name + " " + Description; }
  133.         }
  134.  
  135.         public string RateDec6String
  136.         {
  137.             get  { return Rate.ToString("N6"); }
  138.         }
  139.     }
  140. }
Add Comment
Please, Sign In to add comment