日期:2014-05-18 浏览次数:21113 次
using System; using System.Threading; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private Boolean _bStartThread; private Int64 _total; public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; _bStartThread = false; _total = 0; } private void Run() { if (!_bStartThread) { _bStartThread = true; Thread thread = new Thread(new ThreadStart(ThreadFun)); thread.IsBackground = true; thread.Start(); } } private void ThreadFun() { while (_bStartThread) { Interlocked.Increment(ref _total); this.Text = _total.ToString(); } } private void button1_Click(object sender, EventArgs e) { Run(); } private void button2_Click(object sender, EventArgs e) { _bStartThread = false; } } }