A Simple Window Service With Timer


A Simple Window Service With Timer

A Simple Window Service With Timer


First add Service for Add Items


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.Data.SqlClient;
using System.Configuration;
using System.Timers;
namespace TestWindowsService
{
public partial class Service1 : ServiceBase
{
private System.Timers.Timer timer = null;

public Service1()
{
double interval = 100000;
InitializeComponent();
timer = new System.Timers.Timer(interval);
timer.Elapsed += new ElapsedEventHandler(this.ServiceTimer_Tick);

}

protected override void OnStart(string[] args)
{
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
}

protected override void OnStop()
{
timer.AutoReset = false;
timer.Enabled = false;
}

private void ServiceTimer_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
this.timer.Stop();
SqlCommand MyCmd = new SqlCommand();
DataSet MyDs = new DataSet();
SqlConnection MyCon = new SqlConnection(ConfigurationSettings.AppSettings["MyCon"].ToString());
try
{
MyCmd.Connection = MyCon;
MyCmd.CommandText = "insert into CustOrder (date,price,customer) values ('" + DateTime.Now.ToShortDateString() + "','120','vivek sharma')";
MyCon.Open();
MyCmd.ExecuteNonQuery();

}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
MyCon.Close();
MyCmd.Dispose();
}
this.timer.Start();
}
protected override void OnPause()
{
this.timer.Stop();
}

protected override void OnContinue()
{
this.timer.Start();
}

}
}

Secondly Add AppConfig file from Add Items


App.Config

[?xml version="1.0" encoding="utf-8" ?]
[configuration]
[appSettings]
[add key="MyCon" value="Initial Catalog= test; data source= umar\SQLEXPRESS;integrated security=true"/]
[/appSettings]
[/configuration]


Third Add Installer file from Add Items


Installer.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;

namespace TestWindowsService
{
[RunInstaller(true)]
public partial class Installer1 : Installer
{
private ServiceProcessInstaller serviceProcessInstaller1;
private ServiceInstaller serviceInstaller1;
public Installer1()
{
InitializeComponent();

this.serviceProcessInstaller1 = new ServiceProcessInstaller();
this.serviceInstaller1 = new ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "Service1";
this.serviceInstaller1.StartType = ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
}

}


:Lastly Create Setup Project


After creating setup install it into your system and then register the service.
See Following Attachment for More Details..
Its very simple to creating Window Service.


Attachments

  • SimpleWindowService (37688-1460-TestSolution.rar)
  • Comments

    Author: Mayur26 Nov 2010 Member Level: Gold   Points : 0

    Hi YuDi

    Good Snippets

    Thanks & Regards,
    Mayur Moradiya



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: