Click here to Skip to main content
15,896,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting this error from a call within my code:
string queryStr = "Select Name,PathName,StartMode from Win32_Service Where Name='ArgusService2'";
System.Management.ObjectQuery oQuery = new ObjectQuery(queryStr);
System.Management.ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery);
foreach (System.Management.ManagementObject oService in oSearcher.Get())

At the .Get an exception is thrown and the error listed on the Subject line is returned. It comes back very quickly (.07 seconds).

Any suggestions would be helpful.
Posted

1 solution

The entry method (public void Main()) must return within 30 seconds or the service appears to not start.

Unlike other types of application, returning from the entry method in a Service will not end the process unless there is nothing else running. When I create a service, I will instantiate as little as possible, set up a timer event (or infinite loop in a second thread), then return to the entry method:

C#
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        if (!Debugger.IsAttached)
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Watcher()
            };
            ServiceBase.Run(ServicesToRun);
        }
        else
        {//Just for debugging atm
            (new Watcher()).Start();
            Thread.Sleep(Timeout.Infinite);
        }
    }
}

C#
private static Timer _timer;
       private int _timerIntervalSeconds = 10;
       private static MessageQueueParser _messageQueueParser;
       static BackgroundWorker _backgroundWorker;
       static DataParser _DataParser;

       public Watcher()
       {
           Initialize();

           CanPauseAndContinue = true;
           ServiceName = "Watcher Service";

           _timer = new Timer(_timerIntervalSeconds * 1000);
           _timer.Elapsed += OnTimerElapsed;

           _messageQueueParser = new MessageQueueParser();

           _backgroundWorker = new BackgroundWorker {WorkerSupportsCancellation = false};
           _backgroundWorker.RunWorkerCompleted += WorkerRunWorkerCompleted;
           _backgroundWorker.DoWork += WorkerThreadDoWork;

           //passively recieves api events
           _DataParser = new DataParser();

       }


C#
private static bool _isBusy;

       void OnTimerElapsed(object sender, ElapsedEventArgs e)
       {
           if (_isBusy)
           {
               if (!_backgroundWorker.IsBusy)
                   _isBusy = false;
               return;
           }
           _isBusy = true;

           _backgroundWorker.RunWorkerAsync();

       }


This is an extended example as it's what I'm working on atm. There will be more interaction with the config on startup but the concept is there.

Hope that helps

Don't forget to like and subscribe :P
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900