次の方法で共有


管理オブジェクトのメソッドの実行

管理オブジェクトは、読み取りも変更もできるプロパティを含み、管理クライアント アプリケーションによって呼び出されるメソッドを公開することもできます。たとえば、ディスク オブジェクトが "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 による管理情報へのアクセス | 管理オブジェクトのコレクションの取得 | 管理情報のクエリ | 管理イベントへのサブスクライブと管理イベントの処理 | リモート管理と接続オプション | 厳密に型指定されたオブジェクトの使用