News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  May 30 2015 1:04PM     John
  0 Comments    1235 Views  
In this article John demonstrates, how to create a simple Windows Service application in C#, That writes an Eventlog for every 10 Seconds.
Steps to create Windows service
1. To Create a windows service, open Visual Studio, on the menu bar, choose File, New, Project.

2. In the list of Visual Basic or Visual C# project templates, choose Windows Service, and name the project MyNewService.

3. Choose OK.

4. The project template automatically adds a component class named Service1 that inherits from System.ServiceProcess.ServiceBase.

5.And then right click on Service1.Disigner.cs, click on View Code and paste the following code.
Service1.cs
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;
using System.Threading.Tasks;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        Thread mainThreadObj;

        DateTime currentDateTime;

        Thread objProcessThread;

        public Service1()
        {
            InitializeComponent();
        }

        public void start()
        {

            string[] args = default(string[]);
            OnStart(args);
        }

        protected override void OnStart(string[] args)
        {
            currentDateTime = DateTime.Now;

            mainThreadObj = new Thread(ProcessThread);

            mainThreadObj.Start();

        }

        protected override void OnStop()
        {
        }

        private void ProcessThread()
        {
            if (objProcessThread == null ||
            objProcessThread.ThreadState == System.Threading.ThreadState.Stopped ||
            objProcessThread.ThreadState == System.Threading.ThreadState.Unstarted)
            {


                while (DateTime.Now >= currentDateTime.AddSeconds(10))//Every 10 Secs
                {
                    objProcessThread = new Thread(threadProcessMethod);

                    objProcessThread.IsBackground = true;

                    objProcessThread.Start();

                    currentDateTime = currentDateTime.AddSeconds(10);

                    Thread.Sleep(1000 * 10);

                    ProcessThread();
                }

                Thread.Sleep(1000 * 10); //Every 10 Secs

                ProcessThread();
            }
        }

        private void threadProcessMethod()
        {
            Console.WriteLine(" This Method Will Call Every 10 Seconds : " + currentDateTime);
        }
    }
}
Output
Build the windows services, you can find WindowsService1.exe in output console , some thing like this
D:\MySampleProjects\Service\MySampleWindows_Service\WindowsService1\bin\Debug\WindowsService1.exe
In the Command Prompt window, navigate to the folder that contains your project's output.

For example, under your

D:\MySampleProjects\Service\MySampleWindows_Service\WindowsService1\bin\Debug.
Enter the following command:
installutil.exe WindowsService1.exe
Here you can find steps to install and uninstall the WindowsService
I hope this page will helps you to create first window service in C#.Thanks.
BackToTop
Comments



 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Nov 21 2017 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Nov 8 2017 10:26AM Posted By Pranav
Google URL Shortener in C#
Oct 26 2017 10:43AM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2017 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2017 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2017 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2017 10:20AM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: