日期:2014-05-18 浏览次数:21188 次
delegate void AddTextToControl_delegate(string str);
void Begin()
{
Thread addColorThread = new Thread(new ThreadStart(AddColorToList_M));
addColorThread.Start();
}
void AddTextToRichTextBox(string text)
{
if (rTextBox.InvokeRequired)
{
AddTextToControl_delegate d = new AddTextToControl_delegate(AddTextToRichTextBox);
rTextBox.Invoke(d, new object[] { text});
}
else
{
rTextBox.AppendText(text + "\n");
rTextBox.Select(rTextBox.TextLength, 0);
rTextBox.ScrollToCaret();
}
}
调用 AddTextToRichTextBox(string str)给控件赋值
------解决方案--------------------
声明了一个线程:
private Thread CTrlThread;
声明一个委托:
Delegate void DelegateCTrlThr();
声明了一个方法:
private void CTrlThr()
{
this.listBox1.Items.Add("AAAAA");
}
封装方法:
DelegateCTrlTr objDelegateCTrlTr = new DelegateCTrlTr (CTrlThr);
用INVOKE方法 调用委托:
private void ThreadMethod()
{
this.INVOKE(objDelegateCTrlTr );
}
private void
在按钮事件中启动线程 注意线程封装的方法是用ThreadMethod()方法:
private void button4_Click(object sender, EventArgs e)
{
CTrlThread = new Thread(new System.Threading.ThreadStart(ThreadMethod));
this.CTrlThread.Start();
}