次の方法で共有


EventQuery クラス

WMI (Windows Management Instrumentation) イベント クエリを表します。

この型のすべてのメンバの一覧については、EventQuery メンバ を参照してください。

System.Object
   System.Management.ManagementQuery
      System.Management.EventQuery
         System.Management.WqlEventQuery

Public Class EventQuery
   Inherits ManagementQuery
[C#]
public class EventQuery : ManagementQuery
[C++]
public __gc class EventQuery : public ManagementQuery
[JScript]
public class EventQuery extends ManagementQuery

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

このクラスまたはその派生クラスのオブジェクトは、 ManagementEventWatcher で使用して、WMI イベントにサブスクライブします。できる限り、このクラスのより固有な派生クラスを使用してください。

使用例

 
using System;
using System.Management;

// This sample demonstrates how to subscribe to an event
// using the EventQuery object.

class Sample_EventQuery
{
    public static int Main(string[] args)
    {
        //For this example, we make sure we have an arbitrary class on root\default
        ManagementClass newClass = new ManagementClass(
            "root\\default",
            String.Empty,
            null);
        newClass["__Class"] = "TestWql";
        newClass.Put();

        //Create a query object for watching for class deletion events
        EventQuery eventQuery = new EventQuery("select * from __classdeletionevent");

        //Initialize an event watcher object with this query
        ManagementEventWatcher watcher = new ManagementEventWatcher(
            new ManagementScope("root/default"),
            eventQuery);

        //Set up a handler for incoming events
        MyHandler handler = new MyHandler();
        watcher.EventArrived += new EventArrivedEventHandler(handler.Arrived);

        //Start watching for events
        watcher.Start();

        //For this example, we delete the class to trigger an event
        newClass.Delete();

        //Nothing better to do - we loop to wait for an event to arrive.
        while (!handler.IsArrived) {
             System.Threading.Thread.Sleep(1000);
        }

        //In this example we only want to wait for one event, so we can stop watching
        watcher.Stop();

        //Get some values from the event.
        //Note: this can also be done in the event handler.
        ManagementBaseObject eventArg =
            (ManagementBaseObject)(handler.ReturnedArgs.NewEvent["TargetClass"]);
        Console.WriteLine("Class Deleted = " + eventArg["__CLASS"]);

        return 0;
    }

    public class MyHandler
    {
        private bool isArrived = false;
        private EventArrivedEventArgs args;

        //Handles the event when it arrives
        public void Arrived(object sender, EventArrivedEventArgs e) {
            args = e;
            isArrived = true;
        }
 
        //Public property to get at the event information stored in the handler
        public EventArrivedEventArgs ReturnedArgs {
            get {
                return args;
            }
        }

        //Used to determine whether the event has arrived or not.
        public bool IsArrived {
            get {
                return isArrived;
            }
        }
    }
}
   
[Visual Basic] 
Imports System
Imports System.Management

' This sample demonstrates how to subscribe an event
' using the EventQuery object.

Class Sample_EventQuery
    Public Shared Sub Main()

        'For this example, we make sure we have an arbitrary class on root\default
        Dim newClass As New ManagementClass( _
            "root\default", _
            String.Empty, Nothing)
            newClass("__Class") = "TestWql"
            newClass.Put()

        'Create a query object for watching for class deletion events
        Dim eventQuery As New EventQuery("select * from __classdeletionevent")

        'Initialize an event watcher object with this query
        Dim watcher As New ManagementEventWatcher( _
            New ManagementScope("root/default"), _
            eventQuery)

        'Set up a handler for incoming events
        Dim handler As New MyHandler()
        AddHandler watcher.EventArrived, AddressOf handler.Arrived
   
        'Start watching for events
        watcher.Start()

        'For this example, we delete the class to trigger an event
        newClass.Delete()

        'Nothing better to do - we loop to wait for an event to arrive.
        While Not handler.IsArrived
            Console.Write("0")
            System.Threading.Thread.Sleep(1000)
        End While

        'In this example we only want to wait for one event, so we can stop watching
        watcher.Stop()

        'Get some values from the event
        'Note: this can also be done in the event handler.
        Dim eventArg As ManagementBaseObject = CType( _
            handler.ReturnedArgs.NewEvent("TargetClass"), _
            ManagementBaseObject)
        Console.WriteLine(("Class Deleted = " + eventArg("__CLASS")))

    End Sub

    Public Class MyHandler
        Private _isArrived As Boolean = False
        Private args As EventArrivedEventArgs

        'Handles the event when it arrives
        Public Sub Arrived(sender As Object, e As EventArrivedEventArgs)
            args = e
            _isArrived = True
        End Sub

        'Public property to get at the event information stored in the handler         
        Public ReadOnly Property ReturnedArgs() As EventArrivedEventArgs
            Get
                Return args
            End Get
        End Property

        'Used to determine whether the event has arrived or not.
        Public ReadOnly Property IsArrived() As Boolean
            Get
                Return _isArrived
            End Get
        End Property
    End Class
End Class
   

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Management

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Management (System.Management.dll 内)

参照

EventQuery メンバ | System.Management 名前空間