日期:2014-05-18 浏览次数:20857 次
private object sync = new object(); delegate void SetTextCallback(string text); private void WorkThread() { this.ReadText(); this.WorkThread();//为了循环 } private void ReadText() { int a; lock(sync) { a = Convert.ToInt32(textBox1.Text); } Thread.Sleep(1000); a++; lock(sync) { this.SetText(a.ToString()); } } private void SetText(string text) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.textBox1.Text = text; } }
Thread t = new Thread(new ThreadStart(WorkThread)); t.Start();