Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Same application which runs as a service but also a user launchable program.

Options
  • 20-09-2012 9:15pm
    #1
    Registered Users Posts: 7,501 ✭✭✭


    I came across an application today which ran as a windows service but also if ran by the user launched a GUI.

    I can't find much on how to accomplish this via c#.

    Anyone know?


Comments

  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    You are the worst at Google!

    In main
    if (Environment.UserInteractive)
    {
       Console.WriteLine("Now it's a console app. Or it could be a WinForms or WPF app");
       service.StartInteractive();
       Console.ReadLine();
       service.StopInteractive();
    }
    else
    {
       ServiceBase[] ServicesToRun;
       ServicesToRun = new ServiceBase[] { service };
       ServiceBase.Run(ServicesToRun);
    }
    

    Skeleton service
    internal class MyService : ServiceBase
    {
    
       protected override void OnStart(string[] args)
       {
          // Your startup stuff
          base.OnStart(args);
       }
       
       protected override void OnStop()
       {
          // cleanup
          base.OnStop();
       }
       
       public void StartInteractive()
       {
          OnStart(null);
       }
       
       public void StopInteractive()
       {
          OnStop();
       }
    }
    


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    hummm.

    Thats easier than i expected it to be.

    Surely not the worst at google.
    Im sure people who use yahoo are worse. lol

    Thanks.


Advertisement