using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using System.Reflection; namespace WindowsApplication1 { public partial class Form1 : Form { Thread blah; public Form1() { InitializeComponent(); // define our blah blah = new Thread(DoUpdate); blah.IsBackground = true; // zero our counter updateCounter = 0; } public void startButton1_Click(object sender, EventArgs e) { if (blah.ThreadState != ThreadState.Running) { //start it blah.Start(); } } // declares private int _updateCounter; public int updateCounter { // set our counter get { return _updateCounter; } private set { _updateCounter = value; } } private void DoUpdate() { // so do the update while my counter's under 5k while (updateCounter < 5000) { // if I'm at or for osme reason above 5k, break if (updateCounter >= 5000) { break; } // invoke the update if (this.InvokeRequired) { this.invoke(UpdateInvoker); } // inc it UpdateCounter++; // sleep for some reason Thread.Sleep(10); } } } public void stopButton1_Click(object sender, EventArgs e) { // pause our thread blah.Suspend(); } private MethodInvoker _updateInvoker; public MethodInvoker UpdateInvoker { get { if (_updateInvoker == null) _updateInvoker = new MethodInvoker(delegate() { counterLabel1.Text = UpdateCounter.ToString(); }); return _updateInvoker; } } private Thread _updateThread; { get { return _updateThread; } private set { _updateThread = value; } } } }