using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace SuspendAndTerminateThreadsTest { public partial class frmMain : Form { Mutex singleMutex = new Mutex(); delegate void updateLabel1(); int counter = 0; bool isSuspend1 = false; Thread thread1; public frmMain() { InitializeComponent(); } private void menuItem1_Click(object sender, EventArgs e) { Application.Exit(); } private void frmMain_Load(object sender, EventArgs e) { } private void btnStart1_Click(object sender, EventArgs e) { btnSuspend1.Enabled = true; btnResumn1.Enabled = true; btnTerminate1.Enabled = true; isSuspend1 = false; thread1 = new Thread(new ThreadStart(singleCounter)); thread1.Start(); } private void singleCounter() { while (!isSuspend1) { singleMutex.WaitOne(); this.Invoke(new updateLabel1(setLabel1)); counter++; Thread.Sleep(1000); singleMutex.ReleaseMutex(); } //Thread.CurrentThread.Abort(); } private void btnSuspend1_Click(object sender, EventArgs e) { isSuspend1 = true; } private void setLabel1() { label1.Text = counter .ToString (); } private void btnResumn1_Click(object sender, EventArgs e) { isSuspend1 = false; btnStart1_Click(null, null); } private void btnTerminate1_Click(object sender, EventArgs e) { isSuspend1 = true; counter = 0; thread1.Abort(); btnResumn1.Enabled = false; btnSuspend1.Enabled = false; btnTerminate1.Enabled = false; } } }