Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

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

  • 20-09-2012 09:15PM
    #1
    Registered Users, Registered Users 2 Posts: 7,544 ✭✭✭


    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, Registered Users 2 Posts: 2,062 ✭✭✭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, Registered Users 2 Posts: 7,544 ✭✭✭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