Blocking Method for a Service

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nrworld
    New Member
    • May 2007
    • 7

    Blocking Method for a Service

    Hi,

    In my app, I am registering an event with WMI, and to make it wait for the event, I'm using Console.ReadLin e() for blocking. But it seems that when I use the same code as Service, Console.ReadLin e() 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.ReadLin e().

    Code:
               
                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();
                }
    Last edited by nrworld; Aug 16 '07, 01:50 PM. Reason: Missed few words
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well a service doesn't have a console, so reading a line from it shouldn't work to begin with.
    Why make it a service if you want it to wait for a user input?

    I am pretty sure that you can create an event/delegate for wmi events and stuff.
    (I did one to determine when the network connectivity changed)

    Comment

    • nrworld
      New Member
      • May 2007
      • 7

      #3
      U didn't understand my question. The service is not waiting. I want a suitable blocking call in place of Console.Readlin e.

      Originally posted by Plater
      Well a service doesn't have a console, so reading a line from it shouldn't work to begin with.
      Why make it a service if you want it to wait for a user input?

      I am pretty sure that you can create an event/delegate for wmi events and stuff.
      (I did one to determine when the network connectivity changed)

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by nrworld
        U didn't understand my question. The service is not waiting. I want a suitable blocking call in place of Console.Readlin e.
        Blocking for how long?
        Services aren't supposed to have any user interaction.
        Thread.Sleep() is good for waiting for X amount of time.

        Comment

        Working...