Ascolto di un evento sospeso mediante WMI


Usare il codice seguente per restare in ascolto di una MSBTS_ServiceInstanceSuspendedEvent.

using System.Management;  
      static public void ListenForSvcInstSuspendEvent()  
            // Set up an event watcher and a handler for the MSBTS_ServiceInstanceSuspendedEvent event  
            ManagementEventWatcher watcher = new ManagementEventWatcher( new ManagementScope("root\\MicrosoftBizTalkServer"),  
               new EventQuery("SELECT * FROM MSBTS_ServiceInstanceSuspendedEvent") );  
            watcher.EventArrived += new EventArrivedEventHandler(MyEventHandler);  
            // Start watching for MSBTS_ServiceInstanceSuspendedEvent events  
            while (true)  
         catch (Exception excep)  
            Console.WriteLine("Error: " + excep.Message);  
      static public void MyEventHandler(object sender, EventArrivedEventArgs e)   
         // Print out the service instance ID and error description upon receiving of the suspend event  
         Console.WriteLine("A MSBTS_ServiceInstanceSuspendEvent has occurred!");  
         Console.WriteLine(string.Format("ServiceInstanceID: {0}", e.NewEvent["InstanceID"]));  
         Console.WriteLine(string.Format("ErrorDescription: {0}", e.NewEvent["ErrorDescription"]));  
Option Explicit  
Sub ListenForSvcInstSuspendEvent()  
   Set objWMIServices = GetObject("WinMgmts:{impersonationLevel=impersonate, (security)}\\.\root\MicrosoftBizTalkServer")   
   ' Create the event sink object that receives the events  
   Set sinkSuspInst = WScript.CreateObject("WbemScripting.SWbemSink","SINK_INST_")  
   ' Set up the event selection. SINK_INST_OnObjectReady is called when a MSBTS_ServiceInstanceSuspendedEvent occurs  
   objWMIServices.ExecNotificationQueryAsync sinkSuspInst, "select * from MSBTS_ServiceInstanceSuspendedEvent"  
   ' Infinite loop to wait for event to happen  
   wscript.echo "Waiting for events..."  
End Sub  
Sub SINK_INST_OnObjectReady(objObject, objAsyncContext)  
   wscript.echo "=============================================="  
   wscript.echo "A MSBTS_ServiceInstanceSuspendEvent has occurred!"  
   wscript.echo ""  
   wscript.echo "ServiceInstanceID: " & objObject.InstanceID  
   wscript.echo ""  
   wscript.echo "ErrorDescription: " & objObject.ErrorDescription  
   wscript.echo ""  
End Sub  

Vedere anche

Esempi di script WMI
MSBTS_ServiceInstanceSuspendedEvent (WMI)