In my app, I am registering an event with WMI, and to make it wait for the event, I'm using Console.ReadLine() for blocking. But it seems that when I use the same code as Service, Console.ReadLine() doesn't work. The service is running as SYSTEM. The service terminates without waiting for WMI event. I'm using framework 2.0.
I have tried Thread.Sleep and EventWaitHandle. Those block the event too.
Please suggest a suitable replacement for Console.ReadLine().
Expand|Select|Wrap|Line Numbers
- ManagementOperationObserver managementOperationObserver = new ManagementOperationObserver();
- ConnectionOptions opt = new ConnectionOptions();
- opt.EnablePrivileges = true;
- WqlEventQuery eventQuery = new WqlEventQuery();
- eventQuery.EventClassName = "__InstanceModificationEvent";
- eventQuery.WithinInterval = new TimeSpan(0, 0, 1);
- eventQuery.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5";
- ManagementEventWatcher mewCDEvent = new ManagementEventWatcher(new ManagementScope("root\\CIMV2", opt), eventQuery);
- try
- {
- mewCDEvent.EventArrived += new EventArrivedEventHandler(mewCDEvent_EventArrived);
- mewCDEvent.Start();
- Console.ReadLine(); //doesn't work in Service
- Console.WriteLine("Done wid CD");
- }
- finally
- {
- mewCDEvent.Stop();
- }