Matt23488

C# WinForms Threading

Jul 28th, 2011
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10.  
  11. namespace WinFormsThreading
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         Thread myThread;
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.  
  20.             myThread = new Thread(countDown);
  21.         }
  22.  
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             myThread.Start();
  26.         }
  27.  
  28.         private void countDown()
  29.         {
  30.             for (int i = 5; i >= 0; i--)
  31.             {
  32.                 label1.Invoke(new MethodInvoker(delegate //Invoke to the "dispatcher" that belongs to the UI thread
  33.                 {
  34.                     label1.Text = Convert.ToString(i);
  35.                 }
  36.                 ));
  37.                 Thread.Sleep(1000);
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment