日期:2014-05-18 浏览次数:20749 次
public partial class Form1 : Form { private delegate void UIOperater(string str); Thread thrd; public Form1() { InitializeComponent(); } private void btnOK_Click(object sender, EventArgs e) { //打开一个线程 if (thrd == null || !thrd.IsAlive) { thrd = new Thread(new ThreadStart(ChangeBtnTxt)); thrd.Start(); } } private void ChangeBtnTxt() { uint i = 0; while (true) { i++; i = i % 10000; this.Invoke(new UIOperater(SetBtnTxt), new object[] { i.ToString() }); Thread.Sleep(100); } } private void SetBtnTxt(string str) { this.btnOK.Text = str; } }
------解决方案--------------------
好像写错了一点,改过来
//定义代理ShowStatus private delegate void ShowStatus(string str); ShowStatus showStatus = new ShowStatus(DoShowStatus); //用来更新界面元素的函数 private void DoShowStatus(string txt) { lock (this) { //sta_SysStatus是界面控件 sta_SysStatus.Text = txt; } } //调用 string str = "需要显示的文字"; this.BeginInvoke(showStatus, str);