Reputation: 27952
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?
Upvotes: 2
Views: 3051
Reputation: 8741
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);
}
}
Upvotes: 7
Reputation: 17010
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. :-)
Upvotes: 2
Reputation: 88345
I saw this thread, it might have some more info for you
How to write c# service that I can also run as a winforms program?
Upvotes: 0
Reputation: 88345
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
}
}
}
Upvotes: 2
Reputation: 29956
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.
Upvotes: 1