共用方式為


執行管理物件上的方法

管理物件含有可讀取或變更的屬性,並可公開使用管理用戶端應用程式叫用的方法。例如,磁碟物件可能公開「Format」方法或者服務物件可能使用「Start」和「Stop」方法。下列範例顯示在管理物件上叫用方法的程式碼。在這個特殊狀況中,該方法是靜態的,而且是在類別本身叫用;但通常方法可在執行個體上叫用。

using System;
using System.Management;

public class InvokeMethod {

    public static void Main() {

      //Get the object on which the method will be invoked
      ManagementClass processClass = new ManagementClass("Win32_Process");

      // Option 1: Invocation using parameter objects
      //================================================

      //Get an input parameters object for this method
      ManagementBaseObject inParams = processClass.GetMethodParameters("Create");

      //Fill in input parameter values
      inParams["CommandLine"] = "calc.exe";

      //Execute the method
      ManagementBaseObject outParams = processClass.InvokeMethod ("Create", inParams, null);

      //Display results
      //Note: The return code of the method is provided in the "returnValue" property of the outParams object
      Console.WriteLine("Creation of calculator process returned: " + outParams["returnValue"]);
      Console.WriteLine("Process ID: " + outParams["processId"]);

      // Option 2: Invocation using args array
      //=======================================

      //Create an array containing all arguments for the method
      object[] methodArgs = {"notepad.exe", null, null, 0};

      //Execute the method
      object result = processClass.InvokeMethod ("Create", methodArgs);

      //Display results
      Console.WriteLine ("Creation of process returned: " + result);
      Console.WriteLine ("Process id: " + methodArgs[3]);
    }

}

您也可以非同步執行方法。下列範例將顯示如何進行這項作業:

using System;
using System.Management;

public class InvokeMethodAsync {

    public static void Main() {

      //Get the object on which the method will be invoked
      ManagementClass processClass = new ManagementClass("Win32_Process");

      // Create a results and completion handler
      ManagementOperationObserver handler = new ManagementOperationObserver();
      ObjectReadyHandler objHandler = new ObjectReadyHandler();
        handler.ObjectReady += new ObjectReadyEventHandler(objHandler.NewObject);

      // Invoke method asynchronously
      ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
      inParams["CommandLine"] = "calc.exe";
      processClass.InvokeMethod(handler, "Create", inParams, null);
      
      //Do something while method is executing
      while(!objHandler.IsComplete) {
         System.Threading.Thread.Sleep(1000);
      }

      //After execution is completed, display results
      Console.WriteLine("Creation of calculator process returned: " + objHandler.ReturnObject["returnValue"]);
      Console.WriteLine("Process ID: " + objHandler.ReturnObject["processId"]);

    }

   public class ObjectReadyHandler   {

      private bool isComplete = false;
        private ManagementBaseObject returnObject;

      //Delegate called when the method completes and results are available
      public void NewObject(object sender, ObjectReadyEventArgs e) {
         Console.WriteLine("New Object arrived!");
         ReturnObject = e.NewObject;
         isComplete = true;
      }

      //Property allows accessing the result object in the main function
      public ManagementBaseObject ReturnObject {
         get {
            return returnObject;
         }
      }

      //Used to determine whether the method execution has completed
      public bool IsComplete {
         get {
            return isComplete;
         }
      }
   }
}

請參閱

使用 System.Management 存取管理資訊 | 擷取管理物件集合 | 查詢管理資訊 | 訂閱和使用事件 | 遠端和連接選項 | 使用強式型別物件