如果需要调用,我使用此方法courtesy of casperOne在表单元素上设置属性值。
static void SynchronizedInvoke(ISynchronizeInvoke sync, Action action)
{
// If the invoke is not required, then invoke here and get out.
if (!sync.InvokeRequired)
{
// Execute action.
action();
// Get out.
return;
}
// Marshal to the required thread.
sync.Invoke(action, new object[] { });
}
这有效:
// Set a label's text
SynchronizedInvoke(lblCurrCalStatus, () => lblCurrCalStatus.Text = "Downloading...");
由于显而易见的原因,这不起作用:
// Retrieve a label's text
string calStatus = SynchronizedInvoke(lblCurrCalStatus, () => lblCurrCalStatus.Text);
是否有类似于SynchronizedInvoke
的函数可以返回属性值?这需要一个通用的返回类型和类似的转换:
// Retrieve a label's text
string calStatus = (string)SynchronizedInvokeReturn(lblCurrCalStatus, () => lblCurrCalStatus.Text);
答案 0 :(得分:0)
您可以在匿名方法中设置变量值:
string calStatus = string.Empty;
SynchronizedInvoke(lblCurrCalStatus, () => calStatus = lblCurrCalStatus.Text);