c# - 为许多控件调用委托(delegate)

标签 c# delegates

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/

相关文章:

c# - 通用接口(interface)方法的开放委托(delegate)

c# - 如何在循环中使用匿名方法实例化唯一委托(delegate)(在 C# 中)?

ios - 注销委托(delegate)不起作用

用于日期时间选择器的 C# HttpWebRequest

c# - 从其他应用程序 C# 获取按键

c# - 如何重新初始化数组?

ios - 尝试了解 Swift 中使用协议(protocol)的委托(delegate)的实现

c# - "Uncurrying".NET 中的实例方法

c# - Linq 错误 : "string[] does not contain a definition for ' Except'.“

c# - 如何从 Hangfire 返回一个返回值