Click here to Skip to main content
13,256,716 members (53,389 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Is it possible to run windows service form windows form application programmatically?

I mean.. In one solution, I have two projects: Service, and simple windows app.
In app i have three buttons - "Start", "Install" and "Stop". What should I do?

I must generate exe file from Windows Service? And than somehow run it from app? I need to create Windows Installer in my solution?

Thanks for your help

I would like to build a program that will be installed in the traditional manner by the client - Simply Windows Forms app. One setup file. And.. in this application will be option to start or stop a windows service.

In Visual Studio I've got one soultion, with two projects - I want one setup file.
Posted 28-Apr-11 23:26pm
Updated 28-Apr-11 23:45pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can do it all in one executable. To pass installation parameters, you will need to derive classes System.ServiceProcess.ServiceProcessInstaller and System.ServiceProcess.ServiceInstaller. You only need to implement constructors of your classes.

Installation and uninstallation will be done by System.Configuration.Install.AssemblyInstaller. This class should use the assembly where the classes based on ServiceProcessInstaller and ServiceInstaller are implemented; the implementation will be found in your assembly automatically (which would create a hard-to-detect failure if you have a bug in the implementation). Not too bad though.

Finally, start/stop, etc. are handled by your service code which you create deriving the class System.ServiceProcess.ServiceBase. Triggering this actions is done by Service Controller. To do it programmatically you should use the class System.ServiceProcess.ServiceController.

You need to have a good reading of MSDN on the topics around the classes I listed above, which is well illustrated by code samples. This information is easy to find, a bit harder to understand the work flow and what happens in which process. (A Windows Service process is always a separate one; it takes another process to run installation and Service Controller part.) You can use all this like a cookbook, but I suggest you thoroughly understand it; in this case you won't have any problems.

You can even try to implement it all in one application as I did, but you still need to run it in at least two different processes: one would work in the Windows Service mode, another is must be interactive mode, which can be anything else, such as UI, Console or just invisible batch-mode application.

Important! You code can test System.Environment.UserInteractive during run time to calculate is currently running code is run as Windows Service or not.

now77ak 29-Apr-11 5:37am
Thank you for your reply! I will try it for a moment.. Do you have an example program of that solution?
SAKryukov 29-Apr-11 10:44am
Unfortunately not anything suitable.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Yes, you can do it.

Start, Stop and Restart Windows Service [C#][^]
Windows Services in C#: Controlling Your Service from Another Application (part 6)[^]


Great articles, but i need to install first my service. That articles show me only how to start and stop service.
Come on man, make some effort. Sample:
Simple Windows Service which sends auto Email alerts[^]

MSDN: Details about Windows Service:
Introduction to Windows Service Applications[^]

I hope now you don't say 'Now, I made Windows service, I know how to use it from a C# app but how to install it?'
now77ak 29-Apr-11 5:38am
Great articles, but i need to install first my service. That articles show me only how to start and stop service.
Sandeep Mewara 29-Apr-11 6:45am
Answer updated.

now77ak 29-Apr-11 8:03am
I think you dont understand me.. I know how made, use and install windows service from cmd prompt, using installutil. My service is already done. That Email sample is not what i was thinking. In this sample someone install thi service using installutil.

What I need, is do the same thing.. but programmatically. Like "Start, Stop and Restart Windows Service" sample, but in this sample I must have already installed service. I can only stop and start. In need to install windows service, from my C# "windows form" app.
SAKryukov 29-Apr-11 10:50am
You're right. Installation is separate fro Start/Stop. Basically, installation is updating the registry using AssemblyInstaller. Service Controller works only on already installed Service. I described both parts in my answer. You can use samples from MSDN -- they are good enough (where do you think I learned all that -- MSDN, nothing else). You have a set of classes I mentioned -- go to MSDN pages on each on (will need it anyway); and you will see links to introductory material and code samples. Nothing is too tricky.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 29 Apr 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100