如题怎么跨线程给控件赋值
public partial class Form1 : Form
{
private Thread th1 = null;
private void btn_Start_Click(object sender, EventArgs e)
{
th1 = new Thread(StartSend);
Control.CheckForIllegalCrossThreadCalls = false;
th1.Start();
}
private void StartSend()
{
//这里给控件赋值会抛出异常
try{
tb_SendStates.Invoke(new SetTbText(TbValue), tb_SendStates, "没有数据要发送...");
}
catch()
{}
}
public delegate void SetTbText(TextBox tb, string text);
public void TbValue(TextBox tb,string text)
{
tb.Text = text;
}
private void Btn_Stop_Click(object sender, EventArgs e)
{
string b = th1.ThreadState.ToString();
if (th1.ThreadState.ToString() != "Running")
{
th1.Resume();
}
th1.Abort();
}
}
临摹微笑
相关分类