Matt23488

C# WPF Threading

Jul 28th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 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.Windows.Threading;
  15. using System.Threading;
  16.  
  17. namespace WpfThreading
  18. {
  19.     /// <summary>
  20.     /// Interaction logic for MainWindow.xaml
  21.     /// </summary>
  22.     public partial class MainWindow : Window
  23.     {
  24.         private delegate void SimpleDelegate();
  25.  
  26.         public MainWindow()
  27.         {
  28.             InitializeComponent();
  29.         }
  30.  
  31.         private void button1_Click(object sender, RoutedEventArgs e)
  32.         {
  33.             SimpleDelegate del = new SimpleDelegate(countDown);
  34.             del.BeginInvoke(null, null);//This is called on UI thread, can Invoke directly
  35.         }
  36.  
  37.         private void countDown()
  38.         {
  39.             for (int i = 5; i >= 0; i--)
  40.             {
  41.                 SimpleDelegate del = delegate()
  42.                 { this.label1.Content = i; };
  43.                 this.Dispatcher.BeginInvoke(DispatcherPriority.Send, del);//Called on the Worker thread, have to Invoke from the dispatcher's standpoint
  44.  
  45.                 //Console.WriteLine(i);
  46.                 Thread.Sleep(1000);
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment