/// <summary> /// The Main() method is where we detect if we are a in interactive mode or not. /// If we are not in interactive mode we register the service and allow Service Control Manager (SCM) /// to call the OnStart method either automatically or manually. /// If we are in interactive mode the OnStart() method gets called right away with the args from Main(). /// </summary> /// <param name="args"></param> static void Main(string[] args) { // Create an instance of the class APDoSomethingApp app = new APDoSomethingApp(); // if (args.Length > 0) if (Environment.UserInteractive) { app.OnStart(args); // Trying out shelling out a command line app //int number = 0; //Int32.TryParse(args[0], out number); //app.SendNumber(number); } else { ServiceBase.Run(app); } }
/// <summary> /// The OnStart() method is where we kick of execution of the work to be done. /// If the process is long running, the body of work must be done in a separate thread or process. /// /// Notes: /// For long running processes there will be a main thread listening. Once an event occurs it will /// spin up a new thread or program to handle the event. This should happen regardless of whether we are /// running as a command line program or as a service. When running as a service, OnStop should be used /// to stop the main thread from listening for the event and to release any resources. However, for console /// apps OnStop doesn't get called, the only way to stop a the console app would be to close the console /// window which would kill the thread but will not call OnStop to cleanup any resources. Therefore resource /// management should really be done in the main body of the application. /// /// For short running processes OnStart will contain the main body of work. OnStop will be empty. If running as /// a service, the application should call super.MarkServiceAsStopped() after processing is completed to mark /// the service as stopped. This is not necessary when running interactively. /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { //this.DoParentStuff(); Console.WriteLine("Application running..."); EventLog.WriteEntry("OnStart executed"); if (args.Length > 0) { // Short running int number = 0; Int32.TryParse(args[0], out number); SendNumber(number); EventLog.WriteEntry("Task completed"); } else { // Long running // Should spin up new thread or process APDoSomethingApp forkedApp = new APDoSomethingApp(); Thread listeningThread = new Thread(new ThreadStart(forkedApp.ContinuousSend)); listeningThread.Start(); } }