今天 折腾了好久,才弄好这个,多线程操作 同一个textbox.本来就是菜鸟的我还折腾着用上了委托.
请教大侠们,有没有更简单或更好的方法.谢谢.
private delegate void returnStrDelegate(string str);
private void button1_Click(object sender, EventArgs e)
{
returnCB(textbox1AppendText, "1111");
Thread thread1 = new Thread(new ThreadStart(GetUrl));
thread1.Name = "geturl";
thread1.IsBackground = true; //设为后台线程。
thread1.Start();
}
private void GetUrl()
{
NetClass NC = new NetClass();
//textBox1.Text += "正在查找页面" + "\r\n";
returnCB(textbox1AppendText,"aaa");
}
/// <summary>
/// 向textbox1添加字符
/// </summary>
/// <param name="str"></param>
private void textbox1AppendText(string str)
{
textBox1.AppendText(str);
}
//判断一下是不是该用Invoke
private void returnCB(returnStrDelegate myDelegate,string str)
{
if (this.InvokeRequired)
{
this.Invoke(myDelegate,str);
}
else
{
myDelegate(str);
}
}
private void button1_Click(object sender, EventArgs e)
{
returnCB(textbox1AppendText, "1111");
Thread thread1 = new Thread(new ThreadStart(GetUrl));
thread1.Name = "geturl";
thread1.IsBackground = true; //设为后台线程。
thread1.Start();
}
private void GetUrl()
{
NetClass NC = new NetClass();
//textBox1.Text += "正在查找页面" + "\r\n";
returnCB(textbox1AppendText,"aaa");
}
/// <summary>
/// 向textbox1添加字符
/// </summary>
/// <param name="str"></param>
private void textbox1AppendText(string str)
{
textBox1.AppendText(str);
}
//判断一下是不是该用Invoke
private void returnCB(returnStrDelegate myDelegate,string str)
{
if (this.InvokeRequired)
{
this.Invoke(myDelegate,str);
}
else
{
myDelegate(str);
}
}
请教大侠们,有没有更简单或更好的方法.谢谢.