#region 解决线程间操作无效: 从不是创建控件“label1”的线程访问它问题
delegate void SetTextCallback(string a);//先创建一个委托
/// <summary>
/// 线程间操作无效: 从不是创建控件“label1”的线程访问它
/// </summary>
/// <param name="text">需要赋值给控件的值</param>
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the 线程ID进行比较
// calling thread to the thread ID of the creating thread.调用线程到创建线程的线程ID。
// If these threads are different, it returns true.如果这些线程不同,则返回true。
防止跨线程访问控件
if (this.label1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
//this.label1.Invoke(new Action(()=>{this.label1.Text = text;}))
}
else
{
this.label1
线程间操作无效: 从不是创建控件“label1”的线程访问它。
于 2019-08-07 11:24:55 首次发布