Search code examples
windows-services

How to write a blank service binary for Windows?


I'm trying to create a service that does absolutely nothing for testing purposes. For that I need a binary that does absolutely nothing but services don't seem to start for just any executable, only ones specifically designed to be service binaries. I've tried to find information as to how to make service binaries but can't seem to find anything. Thanks in advance.


Solution

  • Check out the demo service in this little nuget library:

    https://github.com/wolfen351/windows-service-gui
    

    It should give you a good starting point for making a test service to use, it is a trivial implementation of a windows service that does nothing. Also the nuget package will help you run it! :)

    Here is the heart of the code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading;
    
    namespace DemoService
    {
        public partial class Service1 : ServiceBase
        {
            public Service1()
            {
                InitializeComponent();
                Timer t1 = new Timer(AutoStopCallback, null, 15000, -1); // auto stop in 15 seconds for testing
            }
    
            private void AutoStopCallback(object state)
            {
                Stop();
            }
    
            protected override void OnStart(string[] args)
            {
                Thread.Sleep(2000);
                base.OnStart(args);
            }
    
            protected override void OnStop()
            {
                Thread.Sleep(2000);
                base.OnStop();
            }
    
            protected override void OnContinue()
            {
                Thread.Sleep(2000);
            }
    
            protected override void OnPause()
            {
                Thread.Sleep(2000);
            }
    
    
        }
    }