SHARE
TWEET

Untitled

a guest Feb 22nd, 2011 515 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.ComponentModel;
  6. using System.Threading;
  7.  
  8. namespace Calendar
  9. {
  10.     /// <summary>
  11.     /// Logique d'interaction pour MainWindow.xaml
  12.     /// </summary>
  13.     public partial class MainWindow : Window
  14.     {
  15.         BackgroundWorker dater;
  16.         TimeSpan last;
  17.  
  18.         public MainWindow()
  19.         {
  20.             InitializeComponent();
  21.             dater = new BackgroundWorker();
  22.             dater.WorkerReportsProgress = true;
  23.             dater.WorkerSupportsCancellation = true;
  24.             dater.DoWork += new DoWorkEventHandler(dater_DoWork);
  25.             dater.ProgressChanged += new ProgressChangedEventHandler(dater_ProgressChanged);
  26.             dater.RunWorkerCompleted += new RunWorkerCompletedEventHandler(dater_RunWorkerCompleted);
  27.             last = new TimeSpan();
  28.             this.DateLabel.Content = this.last.ToString();
  29.  
  30.            
  31.         }
  32.  
  33.         void dater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  34.         {
  35.            
  36.             this.last += (TimeSpan)e.Result;        
  37.         }
  38.  
  39.         void dater_ProgressChanged(object sender, ProgressChangedEventArgs e)
  40.         {
  41.            
  42.             this.DateLabel.Content = e.UserState.ToString();
  43.         }
  44.  
  45.         void dater_DoWork(object sender, DoWorkEventArgs e)
  46.         {
  47.             BackgroundWorker worker = sender as BackgroundWorker;
  48.             DateTime startDate = DateTime.Now;
  49.            
  50.             while (true)
  51.             {
  52.                 if (worker.CancellationPending)
  53.                     break;
  54.                 else
  55.                     worker.ReportProgress(1, DateTime.Now.Subtract(startDate) + this.last);
  56.                 Thread.Sleep(10);
  57.             }
  58.             e.Result = DateTime.Now.Subtract(startDate);
  59.         }
  60.  
  61.         private void play_Executed(object sender, ExecutedRoutedEventArgs e)
  62.         {
  63.             if (!this.dater.IsBusy)
  64.                 this.dater.RunWorkerAsync();
  65.         }
  66.  
  67.         private void stop_Executed(object sender, ExecutedRoutedEventArgs e)
  68.         {
  69.             if (this.dater.IsBusy)
  70.                 this.dater.CancelAsync();
  71.         }
  72.  
  73.         private void pause_Executed(object sender, ExecutedRoutedEventArgs e)
  74.         {
  75.             if (this.dater.IsBusy)
  76.                 this.dater.CancelAsync();
  77.             this.last = new TimeSpan();
  78.             this.DateLabel.Content = this.last.ToString();
  79.         }
  80.  
  81.         private void CanExecute(object sender, CanExecuteRoutedEventArgs e)
  82.         {
  83.             Control target = e.Source as Control;
  84.             if (target != null)
  85.                 e.CanExecute = true;
  86.             else
  87.                 e.CanExecute = false;
  88.         }
  89.  
  90.     }
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top