【发布时间】:2012-04-20 20:40:34
【问题描述】:
在此链接的代码中:http://c-sharp-programming.blogspot.com/2008/07/cross-thread-operation-not-valid.html,委托用于从工作线程更新文本框的值。
我基本上可以看到发生了什么,但是这行的语法具体是:
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); -
他是说 label1.Invoke() 是代表吗?我没有读过那个。另外,很好地指出,由于 params,您可以在没有显式 object[] 的情况下使用单个参数。
标签: c# multithreading delegates