public void GenerateRandom() { if (!judgeThread) { for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++) { if (i == this.groupBox1.Controls.Owner.Controls.Count - 1) { judgeThread = true; } if (this.groupBox1.Controls.Owner.Controls[i] is TextBox) { if (this.groupBox1.Controls.Owner.Controls[i].InvokeRequired) { setd = GenerateRandom; this.groupBox1.Controls.Owner.Controls[i].Invoke(setd); } else continue; } } } else { for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++) { if (this.groupBox1.Controls.Owner.Controls[i] is TextBox) { // this.groupBox1.Controls.Owner.Controls[i].Text = rd.Next(20).ToString(); //改成下面的写法 this.groupBox1.Controls.Owner.Controls[i].Invoke(new MethodInvoker(() => { this.groupBox1.Controls.Owner.Controls[i].Text = rd.Next(20).ToString(); })); } } } }
你好,大鸟,你的方法确实有用,有个问题,如此写的话 if (!judgeThread) { for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++) { if (i == this.groupBox1.Controls.Owner.Controls.Count - 1) { judgeThread = true; } if (this.groupBox1.Controls.Owner.Controls[i] is TextBox) { if (this.groupBox1.Controls.Owner.Controls[i].InvokeRequired) { setd = GenerateRandom; this.groupBox1.Controls.Owner.Controls[i].Invoke(setd); } else continue; } } } 这段代码都没用了?(这段代码的本意就是让这些控件invoke,而您给我的答案又invoke了) 我想知道我的代码问题出在哪
所有对象都可以放数组里的
public void GenerateRandom()
{
if (!judgeThread)
{
for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++)
{
if (i == this.groupBox1.Controls.Owner.Controls.Count - 1)
{
judgeThread = true;
}
if (this.groupBox1.Controls.Owner.Controls[i] is TextBox)
{
if (this.groupBox1.Controls.Owner.Controls[i].InvokeRequired)
{
setd = GenerateRandom;
this.groupBox1.Controls.Owner.Controls[i].Invoke(setd);
}
else
continue;
}
}
}
else
{
for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++)
{
if (this.groupBox1.Controls.Owner.Controls[i] is TextBox)
{
// this.groupBox1.Controls.Owner.Controls[i].Text = rd.Next(20).ToString();
//改成下面的写法
this.groupBox1.Controls.Owner.Controls[i].Invoke(new MethodInvoker(() =>
{
this.groupBox1.Controls.Owner.Controls[i].Text = rd.Next(20).ToString();
}));
}
}
}
}
if (!judgeThread)
{
for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++)
{
if (i == this.groupBox1.Controls.Owner.Controls.Count - 1)
{
judgeThread = true;
}
if (this.groupBox1.Controls.Owner.Controls[i] is TextBox)
{
if (this.groupBox1.Controls.Owner.Controls[i].InvokeRequired)
{
setd = GenerateRandom;
this.groupBox1.Controls.Owner.Controls[i].Invoke(setd);
}
else
continue;
}
}
}
这段代码都没用了?(这段代码的本意就是让这些控件invoke,而您给我的答案又invoke了)
我想知道我的代码问题出在哪
也可以使用易用的 如 BackgroundWorker