閱讀英文

共用方式為


MethodInvoker 代理人

定義

表示委派,這項委派可執行 Managed 程式碼中任何宣告為 void 且不採用任何參數的方法。

C#
public delegate void MethodInvoker();

範例

下列程式碼範例示範如何使用 MethodInvoker 呼叫方法,以更新應用程式表單的標題列。

C#
public partial class Form1 : Form
{
    public Form1()
    {
        // Create a timer that will call the ShowTime method every second.
        var timer = new System.Threading.Timer(ShowTime, null, 0, 1000);           
    }

    private void ShowTime(object x)
    {
        // Don't do anything if the form's handle hasn't been created 
        // or the form has been disposed.
        if (!this.IsHandleCreated || this.IsDisposed) return;
        
        // Invoke an anonymous method on the thread of the form.
        this.Invoke((MethodInvoker) delegate
        {
            // Show the current time in the form's title bar.
            this.Text = DateTime.Now.ToLongTimeString();
        });
    }
}

備註

MethodInvoker 提供簡單的委派,可用來使用 void 參數清單叫用方法。 對控制項的 Invoke 方法進行呼叫,或當您需要簡單的委派,但不想自行定義委派時,可以使用此委派。

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10