0

What would be a short example with a Windows service and how to install and run it?

I've searched on the Internet, but what I've tried didn't have anything written on the On Start method. Plus, when I've tried to install it the error OpenSCManager keeps popping up.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
elisa
  • 743
  • 2
  • 13
  • 31
  • a Windows Service can be just a `dll` with the start of the application on the event `OnStart`. Care to show us more what are you trying to do? – balexandre Mar 28 '11 at 07:56

3 Answers3

3
  1. Find install util at C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe

  2. Then run InstallUtil.exe "c:\myservice.exe"

  3. Go to services.msc and then locate and start your service

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Brijesh Mishra
  • 2,738
  • 1
  • 21
  • 36
  • i can't find install util. is there another way to test the application? – elisa Mar 28 '11 at 09:36
  • if you want to debug your windows service follow this step 1>add app.config 2>add this code snippet in your service.cs public static void Main() { #if (!DEBUG) ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new MyServiceClassName() }; Run(ServicesToRun); #else var service = new MyServiceClassName(); service.OnStart(null); System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); #endif } – Brijesh Mishra Mar 28 '11 at 09:47
  • which version of .net are you using? maybe download it from web – Brijesh Mishra Mar 28 '11 at 09:53
2

Here are some examples about how to write and install a Windows service in C#:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kamyar
  • 18,639
  • 9
  • 97
  • 171
0

My answer to this question gives you step-by-step instructions for creating a Windows service in C#.

My answer to this question shows you have to modify the service so that it can install and uninstall itself from the command line.

InstallUtil.exe has been part of .NET since 1.1, so it should be on your system. However, you likely can't use it from a 'normal' command prompt. If you have Visual Studio installed, open the Visual Studio command prompt. That'll define the appropriate environment variables that make InstallUtil accessible without path information.

The OnStart() callback gives you an opportunity to start the business logic of your service. If you don't do anything in the OnStart() callback, your service will immediately shut down. Typically, you'll start a thread that performs the work you're interested in. Here is a small example to show you what it looks like.

private static System.Timers.Timer _timer;

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    // Write a message to the event log.
    string msg = String.Format("The Elapsed event was raised at {0}", e.SignalTime);
    EventLog.WriteEntry(msg, EventLogEntryType.Information);
}

protected override void OnStart(string[] args)
{
    // Create a timer with a 10-econd interval.
    _timer = new System.Timers.Timer(10000);

    // Hook up the Elapsed event for the timer.
    _timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

    // Signal the timer to raise Elapsed events every 10 seconds.
    _timer.Start();
}

protected override void OnStop()
{
    // Stop and dispose of the timer.
    _timer.Stop();
    _timer.Dispose();
}

Doing something like this will effectively keep your service running until it shuts down. Hope this helps.

Community
  • 1
  • 1
Matt Davis
  • 45,297
  • 16
  • 93
  • 124