views:

1714

answers:

6

I'd like to create an application using C# that...:

  • Can be run as a Windows application, with a GUI (it'll indicate progress, status, etc.)

    OR

  • Can be run as a Windows service, without a GUI

Is it possible? Can someone get me started?

I guess the alternative is that I could create a Windows service, and then a separate GUI application which can poll the Windows service to fetch data from it (progress, status, etc.).

In that case... how can I fetch data from the Windows service from my GUI application?

A: 

http://www.google.com/search?complete=1&hl=en&q=run+windows+app+as+service&btnG=Google+Search&aq=0&oq=run+windows+app+as+

Jacob Adams
GIYF answers are not welcome here.
Geoffrey Chetwood
If the answer was easy to find with Google, then they would not have come to SO
benPearce
When I googled it, there were several links on the first page that seemed to solve the problem mentioned. Had a I just posted those links, this response probably would have been upvoted instead of downvoted. Seems a little ridiculous.
Jacob Adams
+5  A: 

I'm doing something similar to what you're asking for. I have programmed it so that if you send the command line parameter "/form" to the executable, it will pop up a windows form instead of running as a service.

As far as running the background job itself, in both cases you will need to do some sort of threading (perhaps with a timer) to do the work and report status back to your form asynchronously. This would be a whole different discussion topic on creating threaded GUI apps.

The "form or service" code goes something like this:

static class Program
{
 /// <summary>
 /// The main entry point for the application.
 /// </summary>
 private static void Main(string[] args)
 {
  if (args.Length > 0 && args[0] == "/form")
  {
   var form = new MainForm();
   Application.Run(form);
   return;
  }

  var ServicesToRun = new ServiceBase[]
         {
          new BackgroundService()
         };

  ServiceBase.Run(ServicesToRun);
 }
}
jeremcc
+1  A: 

You should go with the latter option. Create your service and then a seperate GUI app. Most of the plumbing for doing all this is already provided for you in the framework. Have a look at the ServiceController class.

AdamRalph
This option sounds the most promising so far... but I need to do more than just stop, start, and pause the service. I need to be able to send the service specific commands to make it do other stuff, and send commands to retrieve data from the service...Can ServiceController do this?
Keith Palmer
+1 This is probably the best idea imo. In my case, All of the settings for my services are stored in an xml document. My services all run on timers and each time the event triggers, it checks the settings file.
SnOrfus
+1  A: 

I've never done an app that can be run as a windows service or a GUI, but we have a lot of apps that you can switch between console app and windows service using a compiler flag. (I just saw the answer with the cmd line arg - that might even be better!)

We usually then just use a compiler flag to switch between the two. Here's an example... I didn't completely think this through, but it might give you a start:

#define RUN_AS_SERVICE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ServiceProcess;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
#if RUN_AS_SERVICE

            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[]
            {
                new MyService()
            };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
            // Run as GUI
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
#endif
        }
    }
    public class MyService : ServiceBase
    {
        protected override void OnStart(string[] args)
        {
            // Start your service
        }

        protected override void OnStop()
        {
            // Stop your service
        }
    }
}
Andy White
A: 

I saw this thread, it might have some more info for you

http://stackoverflow.com/questions/421516/how-to-write-c-service-that-i-can-also-run-as-a-winforms-program

Andy White
A: 

Build the actual application in libraries. You can then add any UI (I say this loosely, as service is not really a UI) you desire. As long as you think of the app as a windows aplication and a service application, you are developing two applications. If you think of the application as the business problem it solves, you will then think of the windows forms UI and the service UI, which is much saner for your needs.

While this may sound sane, you would be surprised how many applications need a complete overwrite to become a different UI type. thank you for the question. It convinces me there is a need for the book I am writing. :-)

Gregory A Beamer