You can asynchronously call a method using WMI in .NET Framework by passing in an instance of the ManagementOperationObserver class into the InvokeMethod method. By calling a method asynchronously, you can complete other tasks while the method is being called and executed. If you call the method semisynchronously, you must wait for the method to finish executing before you start any other tasks. For information on calling a method semisynchronously, see How To: Execute a Method.
Example
The following code calls a method asynchronously. The Win32_Process.Create method is called to create a new process for Calc.exe.
Imports System
Imports System.Management
Public Class InvokeMethodAsync
Public Sub New()
' Get the object on which the method
' will be invoked
Dim processClass As ManagementClass = _
New ManagementClass("Win32_Process")
' Create a results and completion handler
Dim handler As ManagementOperationObserver = _
New ManagementOperationObserver
AddHandler handler.Completed, _
AddressOf Completed
' Invoke method asynchronously
Dim inParams As ManagementBaseObject = _
processClass.GetMethodParameters("Create")
inParams("CommandLine") = "calc.exe"
processClass.InvokeMethod( _
handler, "Create", inParams, Nothing)
' Do something while method is executing
While (Not isComplete)
System.Threading.Thread.Sleep(1000)
End While
End Sub
Private isComplete As Boolean = False
Private returnObj As ManagementBaseObject
' Delegate called when the method completes
' and results are available
Private Sub NewObject(ByVal sender As Object, _
ByVal e As ObjectReadyEventArgs)
Console.WriteLine("New Object arrived!")
returnObj = e.NewObject
End Sub
Private Sub Completed(ByVal sender As Object, _
ByVal e As CompletedEventArgs)
isComplete = True
End Sub
Public Overloads Shared Function _
Main(ByVal args() As String) As Integer
Dim asyncMethod As New InvokeMethodAsync
End Function
End Class
using System;
using System.Management;
public class InvokeMethodAsync
{
public InvokeMethodAsync()
{
// 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();
handler.Completed +=
new CompletedEventHandler(Completed);
// 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(!isComplete)
{
System.Threading.Thread.Sleep(1000);
}
}
private bool isComplete = false;
private ManagementBaseObject returnObject;
// Delegate called when the method completes
// and results are available
private void NewObject(object sender,
ObjectReadyEventArgs e)
{
Console.WriteLine("New Object arrived!");
returnObject = e.NewObject;
}
private void Completed(object sender,
CompletedEventArgs e)
{
isComplete = true;
}
public static void Main()
{
InvokeMethodAsync wmiMethod = new InvokeMethodAsync();
}
}
Compiling the Code
See Also