在此链接的代码中:http://c-sharp-programming.blogspot.com/2008/07/cross-thread-operation-not-valid.html,委托用于从工作线程更新文本框的值。解释委托调用的语法c#
我基本上可以看到发生了什么,但此行的语法明确:
label1.Invoke(del, new object[] { newText });
是混乱给我。有人可以解释吗?当只有一个参数时,为什么我们为委托使用新的对象数组语法?
全码:
delegate void updateLabelTextDelegate(string newText);
private void updateLabelText(string newText)
{
if (label1.InvokeRequired)
{
// this is worker thread
updateLabelTextDelegate del = new updateLabelTextDelegate(updateLabelText);
label1.Invoke(del, new object[] { newText });
}
else
{
// this is UI thread
label1.Text = newText;
}
}
这是一个错误,label1.Invoke()不是委托。只要写'label1.Invoke(del,newText);' – 2012-04-20 21:12:55
他说label1.Invoke()是一个委托吗?我没有读到。另外,很好的指出你可以使用一个单独的参数,而不需要显式对象[],这要归功于params。 – payo 2012-04-20 21:19:06