Search This Blog

Saturday 31 March 2018

Windows services in c#


Step 1: - Open Visual Studio 2015 => Goto File Menu => New => Project...

Step 2: - In the Installed Templates list, select Visual C# => Expand Windows => Select Classic Desktop => Select Windows Service from middle pane=> Type WindowsService in the Name box and click OK


Step 3: - Open Service1.cs => Right Click Empty area =>Click View Code or Click link “click here to switch to code view




Complete Code

using System;
using System.ServiceProcess;
using System.Timers;

namespace WindowsService
{
    public partial class Service1 : ServiceBase
    {
        Timer timer = null;
        public Service1()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            Console.WriteLine("Timer Started " + DateTime.Now.ToString());

            timer = new Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;
            timer.Enabled = true;
        }
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine("Timer_Elapsed is called " + DateTime.Now.ToString());
        }
        protected override void OnStop()
        {
            timer.Enabled = false;
            Console.WriteLine("Timer Stopped " + DateTime.Now.ToString());
        }
    }
}

Step 4: - Open Service1.cs => Right Click Empty area => Click Add Installer



Step 5: - Right Click ServiceInstaller1 => Click Properties


If you wish, change the ServiceName. In this project I am not changing the ServiceName.

Step 6: - Right Click serviceProcessInstaller1=> Click Properties => Change Account User to LocalService


Step 7: - Right Click WindowsService Project=> Click Build

Window Service created successfully. Let’s create installer for service.

Step 1: - Right Click on Solution ‘WindowsService’ (1 project) => Add => New Project… => Select Other Project Types from left pane=> Select Visual Studio Installer => Select Setup Project from middle pane=> Type WindowsService in the Name box and click OK




Note: -

If Visual Studio Installer template missing in your visual studio 2015. Click link to download Visual Studio Installer.

Step 2: - Right Click WindowsServiceSetup => Add => Click Project Output… => Select Primary output => Click OK




Step 2: - Right Click WindowsServiceSetup => View => Click Custom Actions



Step 3: - Right Click Custom Actions => Click Add Custom Action… => Open Application Folder => Select Primary output from WindowsService (Active) => Click OK





Step 4: - Right Click WindowsServiceSetup => Click Build

Setup is ready to install.

Step 1: - Open Setup folder i.e. reside in WindowsServiceSetup project Debug folder => Click setup.exe to install it.

Step 2: - Open Services windows type services.msc in search box and press Enter


Step 3: - Select Service1 from list => Click Start

Add Done


To Run Scheduler on specified time do following changes in your code.

using System;
using System.ServiceProcess;
using System.Timers;

namespace WindowsService
{
    public partial class Service1 : ServiceBase
    {
        Timer timer = null;
        DateTime scheduledTime = DateTime.MinValue;

        public Service1()
        {
            InitializeComponent();
            // Set Scheduler to run every day at 07:00 am
            scheduledTime = DateTime.Parse("07:00");
        }
        protected override void OnStart(string[] args)
        {
            Console.WriteLine("Timer Started " + DateTime.Now.ToString());

            timer = new Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;
            timer.Enabled = true;
        }
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (DateTime.Now > scheduledTime)
            {
                Console.WriteLine("Timer_Elapsed is called " + DateTime.Now.ToString());

                // Set Scheduler to run next day at 07:00 am
                scheduledTime = scheduledTime.AddDays(1);
            }
        }
        protected override void OnStop()
        {
            timer.Enabled = false;
            Console.WriteLine("Timer Stopped " + DateTime.Now.ToString());
        }
    }
}

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete