Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#

Working with Windows Service Using VS 2005

Rate me:
Please Sign up or sign in to vote.
2.25/5 (4 votes)
16 Jun 2009CPOL5 min read 27.6K   12  
Written in C# for beginners who want to learn
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace MyService
{
    public partial class MyWinService : ServiceBase
    {
        public MyWinService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            const Int32 iTimer_Interval = 60000;
            Timer oTimer;
            System.IO.File.AppendAllText("C:\\AuthorLog.txt", "MyService has been started at " + DateTime.Now.ToShortTimeString());
            System.Threading.TimerCallback tDelegate = EventAction;
            oTimer = new System.Threading.Timer(tDelegate, this, 0, iTimer_Interval);
        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
           
        }

        public void EventAction(object sender)
        {
            System.IO.File.AppendAllText("C:\\AuthorLog.txt", "MyService fires EventAction at " + DateTime.Now.ToShortTimeString());
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Divyalok Suman has done B.E in Computer Science from bangalore ,Karnataka, India . Past 6 years he is working on web-based and windows based solutions in Asp.net MVC 1.0/2.0/3.0/4.0, ASP.NET 2.0/3.5/4.0, C# 2.0/3.5/4.0, AJAX, Web Services, MS SQL Server 2005/2008,Win Forms, Window services. He is also an MCP and MCTS . He has good knowledge of Object Oriented Programming, 3-Tier Architecture and Designing.

Comments and Discussions