C# 为许多控件调用委托

C# 为许多控件调用委托,c#,delegates,C#,Delegates,C#2008 SP1 下面的函数将从另一个线程调用。因此,必须调用控件本身,以便创建它们的正确线程可以更改属性 但是,由于我有许多控件需要更新。我真的不想为每个人写所有这些代表。我在下面做了一个。然而,我认为这是很多代码。有没有办法缩短这个 非常感谢, public void SetIdleState(string callStatusMsg) { this.btnCallAnswer.Text = CATWinSIP_MsgStrings.Call; t

C#2008 SP1

下面的函数将从另一个线程调用。因此,必须调用控件本身,以便创建它们的正确线程可以更改属性

但是,由于我有许多控件需要更新。我真的不想为每个人写所有这些代表。我在下面做了一个。然而,我认为这是很多代码。有没有办法缩短这个

非常感谢,

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 });

    }
我问了一个类似的问题。Jon Skeet提供的答案是我遇到的最好的方法。相关代码如下

创建静态辅助对象方法:

public static void InvokeIfNecessary(UIElement element, MethodInvoker action)
{
    if (element.Dispatcher.Thread != Thread.CurrentThread)
    {
        element.Dispatcher.Invoke(DispatcherPriority.Normal, action);
    }
    else
    {
        action();
    }
}
在您的示例中,您可以将其用作:

InvokeIfNecessary(statusDisplay1, delegate {statusDisplay1.CallStatus = callStatusMsg;});

以下是我在上一个项目中所做的工作:

我编写了一个helper静态类

public static class WorkbenchService
{    
    private static SynchronizationContext uiContext;
static WorkbenchService()
{
    uiContext = WindowsFormsSynchronizationContext.Current;
}

/// <summary>
    /// Makes a call GUI threadSafe. WARNING: This method waits for the result of the operation, which can result in a dead-lock when the main thread waits for this thread to exit!
    /// </summary>
    public static void SafeThreadCall(SendOrPostCallback d, object state)
    {
        uiContext.Send(d, state);
    }
    /// <summary>
    /// Makes a call GUI thread safe without waiting for the returned value.
    /// </summary>
    public static void SafeThreadAsyncCall(SendOrPostCallback d, object state)
    {
        uiContext.Post(d, state);
    }
}

我找到了最好的方法。把我的代码转换成这个

希望这对其他人有帮助

 // Delegate for marshalling the call on the correct thread.
    private delegate void SetIdleStateDelegate(string callStatusMsg);

    // Set object back to idle state.
    public void SetIdleState(string callStatusMsg)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new SetIdleStateDelegate(SetIdleState), callStatusMsg);
        }
        else
        {
            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;
        }           
    }      

不知道我是否能解释清楚。但我需要更改该函数中的所有控件。例如,我做了一个statusDislay控件来显示我是如何执行此操作的。但是,我不想为每个控件编写相同的代码。i、 e.tbnCallNumber、btnEndCallReject、btnHoldUnhold等。
 // Delegate for marshalling the call on the correct thread.
    private delegate void SetIdleStateDelegate(string callStatusMsg);

    // Set object back to idle state.
    public void SetIdleState(string callStatusMsg)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new SetIdleStateDelegate(SetIdleState), callStatusMsg);
        }
        else
        {
            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;
        }           
    }