private delegate void SetShootRecoordTextCallback(string text);
//在给textBox1.text赋值的地方调用以下方法即可
private void SetShootRecoordText(string text)
{
// InvokeRequired需要比较调用线程ID和创建线程ID
// 如果它们不相同则返回true
if (this.textBox_ShootRecoord.InvokeRequired)
{
SetShootRecoordTextCallback d = new SetShootRecoordTextCallback(SetShootRecoordText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox_ShootRecoord.Text = text;
textBox_ShootRecoord.SelectionStart = textBox_ShootRecoord.Text.Length;
textBox_ShootRecoord.ScrollToCaret();
}
}
//在给textBox1.text赋值的地方调用以下方法即可
private void SetShootRecoordText(string text)
{
// InvokeRequired需要比较调用线程ID和创建线程ID
// 如果它们不相同则返回true
if (this.textBox_ShootRecoord.InvokeRequired)
{
SetShootRecoordTextCallback d = new SetShootRecoordTextCallback(SetShootRecoordText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox_ShootRecoord.Text = text;
textBox_ShootRecoord.SelectionStart = textBox_ShootRecoord.Text.Length;
textBox_ShootRecoord.ScrollToCaret();
}
}
本文介绍了一种在C#中实现跨线程安全更新UI文本框的方法。通过使用委托和Invoke方法确保了在非主线程中也能正确地更新界面元素的文本内容。

1049

被折叠的 条评论
为什么被折叠?



