C# 2008 SP1
下面的函数将从另一个线程调用。因此,必须调用控件本身,以便创建它们的正确线程可以更改属性。
但是,因为我有很多控件需要更新。我真的不想为每一位委托(delegate)写下所有这些委托(delegate)。我在下面做了一个。但是,我认为这是很多代码。有什么办法可以缩短这个时间吗?
非常感谢,
public void SetIdleState(string callStatusMsg)
{
this.btnCallAnswer.Text = CATWinSIP_MsgStrings.Call;
this.btnEndCallReject.Text = CATWinSIP_MsgStrings.EndCall;
this.btnHoldUnhold.Text = CATWinSIP_MsgStrings.Hold;
this.btnCallAnswer.Enabled = true;
this.btnRedial.Enabled = true;
this.btnEndCallReject.Enabled = false;
this.btnHoldUnhold.Enabled = false;
if (this.statusDisplay1.InvokeRequired)
{
statusDisplay1.Invoke(new UpdateCallStatusDelegate(this.UpdateCallStatus), callStatusMsg);
}
else
{
this.statusDisplay1.CallStatus = callStatusMsg;
}
}
// Delegate for marshalling the call on the correct thread.
private delegate void UpdateCallStatusDelegate(string callStatusMsg);
private void UpdateCallStatus(string callStatusMsg)
{
this.statusDisplay1.CallStatus = callStatusMsg;
}
最佳答案
怎么样:
Dispatcher.BeginInvoke(new DispatcherOperationCallback((param) =>
{
this.statusDisplay1.CallStatus = callStatusMsg;
return null;
}), DispatcherPriority.Background, new object[] { null });
}
关于c# - 为许多控件调用委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/855985/