Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.ComponentModel;
- using System.Collections.ObjectModel;
- namespace WpfLongThread
- {
- public partial class MainWindow : Window
- {
- ObservableCollection<Currency> currencies = new ObservableCollection<Currency>();
- public ObservableCollection<Currency> Currencies { get { return currencies; } }
- private BackgroundWorker bw = new BackgroundWorker();
- public MainWindow()
- {
- currencies = new ObservableCollection<Currency>();
- currencies.Add(new Currency { Name = "TST", Rate = 987.654321M, Description = "Initial dummy testdata" });
- InitializeComponent();
- bw.WorkerReportsProgress = true;
- bw.WorkerSupportsCancellation = true;
- bw.DoWork += new DoWorkEventHandler(bw_DoWork);
- bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
- bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
- }
- private void Thread_Click(object sender, RoutedEventArgs e)
- {
- if (bw.IsBusy == true)
- {
- this.statusText.Text = "Already running: Please wait";
- return;
- }
- while (currencies.Count > 0) {
- currencies.RemoveAt(0);
- }
- bw.RunWorkerAsync();
- }
- private void Cancel_Click(object sender, RoutedEventArgs e)
- {
- if (bw.WorkerSupportsCancellation == true && bw.IsBusy == true)
- {
- bw.CancelAsync();
- return;
- }
- this.statusText.Text = " ";
- }
- private void bw_DoWork(object sender, DoWorkEventArgs e)
- {
- BackgroundWorker worker = sender as BackgroundWorker;
- System.Threading.Thread.Sleep(200);
- worker.ReportProgress(-5, "initializing... ...");
- System.Threading.Thread.Sleep(700);
- worker.ReportProgress(-1, "running ... ...");
- for (decimal i = 1; (i <= 20); i++)
- {
- if ((worker.CancellationPending == true))
- {
- e.Cancel = true;
- break;
- }
- else
- {
- // Perform a time consuming operation and report progress.
- System.Threading.Thread.Sleep(700);
- string no2 = i.ToString("00");
- Currency tempCurr = new Currency() { Name = "C" + no2, Rate = 10+5*i-(i/7) , Description = "Description no " + no2 };
- worker.ReportProgress((int)(i * 5), tempCurr);
- }
- }
- }
- private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- if (e.ProgressPercentage < 0)
- {
- this.statusText.Text = e.UserState.ToString();
- return;
- }
- this.statusText.Text = e.ProgressPercentage.ToString() + "% ";
- if (e.UserState.GetType() == typeof(WpfLongThread.Currency))
- {
- currencies.Add((Currency)(e.UserState));
- }
- }
- private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- if ((e.Cancelled == true))
- {
- this.statusText.Text = "Canceled!";
- }
- else if (!(e.Error == null))
- {
- this.statusText.Text = ("Error: " + e.Error.Message);
- }
- else
- {
- this.statusText.Text = "Done!";
- }
- }
- private void AddRow_Click(object sender, RoutedEventArgs e)
- {
- this.statusText.Text = " ";
- currencies.Add(new Currency { Name = "ABC", Rate = 123.456789M, Description = "From Add button at " + DateTime.Now });
- }
- }
- public sealed class Currency
- {
- public string Name { get; set; }
- public string Description { get; set; }
- public decimal Rate { get; set; }
- public string NameAndDescription
- {
- get { return Name + " " + Description; }
- }
- public string RateDec6String
- {
- get { return Rate.ToString("N6"); }
- }
- }
- }
Add Comment
Please, Sign In to add comment