0

I want to create windows service and for that i create the process and installer program also.

It installed well and I was ready to start my next move,but after starting the service it doesn't show anything.

I used this reference.

http://tech.pro/tutorial/895/creating-a-simple-windows-service-in-csharp

This is my program.cs file.

namespace WindowsService
{
    class Program : ServiceBase
    {
        static void Main(string[] args)
        {
            ServiceBase.Run(new Program());
        }
        public Program()
        {
            this.ServiceName = "Battery Service";
        }

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            Console.WriteLine("Hello World");

            Console.ReadLine();
        }

        protected override void OnStop()
        {
            base.OnStop();


        }
    }
}

And next one is my installer file,WindowsServiceInstaller.cs

namespace WindowsService
{
    [RunInstaller(true)]
    public class WindowsServiceInstaller:Installer
    {
        public WindowsServiceInstaller()
        {
            var processInstaller = new ServiceProcessInstaller();
            var serviceInstaller = new ServiceInstaller();

            //set the privileges
            processInstaller.Account = ServiceAccount.LocalSystem;

            serviceInstaller.DisplayName = "Battery Service";
            serviceInstaller.StartType = ServiceStartMode.Manual;

            //must be the same as what was set in Program's constructor
            serviceInstaller.ServiceName = "Battery Service";

            this.Installers.Add(processInstaller);
            this.Installers.Add(serviceInstaller);
        }
    }
}
  1. Service is installed correctly as per the above reference link.
  2. Service appears in the Administrative Tools/Services and i started it.
  3. It also appears on the Task Manager. Though,not executing the code.

Help me.

  • 2
    How do you know it is not executing? I doubt there is a visible stdin/stdout assigned to services. Have you tried something like writing to a file? – Gábor Bakos Apr 05 '15 at 06:57
  • possible duplicate of [Console.WriteLine() inside a Windows Service?](http://stackoverflow.com/questions/8792978/console-writeline-inside-a-windows-service) – Philip Stuyck Apr 05 '15 at 07:10
  • 1
    Google "session 0 isolation" to learn why services cannot display info on your desktop. Use a log file. – Hans Passant Apr 05 '15 at 08:17
  • See https://stackoverflow.com/questions/8792978/console-writeline-inside-a-windows-service/63685682#63685682 on how to open a console window. Note that this only makes sense when run manually (not as windows service) – TomB Sep 01 '20 at 13:24

2 Answers2

0

There is no interactive console for a Windows service. Try using Debug.WriteLine instead and monitoring with an app like DebugView

Jon Tirjan
  • 3,556
  • 2
  • 16
  • 24
0

Have a look at this article from MSDN How to: Debug Windows Service Applications.

You basically have to create the service in Debug configuration, start it as a service, then attach the VS debugger to the service process. You will then be able to debug it normally.

Because you need to debug the service when it starts, the article also give tips on how you can delay the execution of OnStart() to give you time to attach the debugger.

Renaud Bompuis
  • 16,596
  • 4
  • 56
  • 86