views:

378

answers:

6

I have 3 Windows Service Question

  1. Is WS can work in background ? Is it possible to do some job every 2 minutes ? (if yes, can I get some help ?)

  2. How can I install WS in simple way ? (not with Installutil.exe .......)

  3. How can I run .exe file from Windows service ?

I've tried this way:

System.Diagnostics.Process G = new Process();
    G.StartInfo.FileName = @"d:\demo.exe";
    G.Start();

but it didn't work.

+6  A: 
boj
+4  A: 

Windows Services should work in the background, by default. They are expected to return control over to the service manager immediately after starting, so typically you would fire off a thread to handle your service tasks.

That being said, the System.Threading.Timer class should work well for starting a timer that runs on a regular interval.

As for installation, you should use Installutil.exe - this is the standard way to install a Windows Service.

I do recommend taking a look at this codeproject article. It has a good, clean template which you could use. In addition, there is a batch file example to simplify the use of Installutil, which may make it "nicer" to you.

Reed Copsey
+2  A: 

If you are developing with VS2008, then you have a template available for creating Windows Services.

Basically, you will create a Timer (System.Windows.Timers.Timer), do your initialization on the OnStart method, do clean and finalize stuff on the OnStop method, and initialize the Timer (on OnStart, or with a OnCommand)

You can look at this for a basic example or here, for an example with timers

For the installation, besides the options in another answers, you can also create an Installer Project, again, check the template projects

Jhonny D. Cano -Leftware-
+2  A: 
  1. Yes a windows service can and does work in the background. To do the same job every 2 minutes use the system.Timer class and put your code in the onElapsed event. I've recently created this type of service and found there are two types of Timer you can use, make sure you use the correct one or you will not find the onElapsed event.

  2. I've not tried installing without the InstallUtil.exe but I do have a .bat file that I use which is run as part of my main application installation.

Your follow-up question on running an .exe from a Windows Service, to run an .exe from a Windows Service I have used:

Process p = new Process();
p.StartInfo.WorkingDirectory = @"C:\";
p.StartInfo.FileName = @"C:\demo.exe";
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();

Remember that the executable will run at the same level as the service which means it can't display anything on the desktop. If you are expecting to see any window open or the .exe requires any user input then you will be disappointed and the .exe may wait indefinitely. (I found this link Launch external programs helped and there is also this question on SO - Launching an Application (.EXE) from C#)

Swinders
+1  A: 

You can also use sc.exe to install your app as a service, but it's really not too much different than InstallUtil.exe for installation. It does provide more control over services from the command line.

Here is an older article describing sc.exe's use. Also just running sc from a command prompt will show it's possible commands.

Joe Doyle
A: 

Just as an alternate thought, you could schedule a task to run every two minutes if creating a service is too hard.

Joel Lucsy