日期:2014-05-16 浏览次数:21072 次
//跨线程安全访问控件委托方法
delegate void SetTextCallback(string text);
private Thread doYaohao =null;
//要显示的随机数
private string[] word = new string[] { "A01", "B02", "C03", "D04", "E05", "F06", "G07", "H08", "I09", "J10" };
//开始按钮
private void button1_Click(object sender, EventArgs e)
{
this.doYaohao = new Thread(new ThreadStart(this.ThreadProcSafe));
this.doYaohao.IsBackground = true;
this.doYaohao.Start();
}
private void ThreadProcSafe()
{
Random rd = new Random();
int i = rd.Next(0, 9);
while (true)
{
i = rd.Next(0, 9);
this.SetText(word[i]);
Thread.Sleep(10);
}
}
//委托方法
private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
&nb