Tuesday 18 December 2018

Window service in .net c# with Timer

///////////////// 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;
using System.Configuration;
namespace SendMailTimer
{
    public partial class SendMails : ServiceBase
    {
        Timer _tmr;
        static bool timerVal = true;

        public SendMails()
        {
            InitializeComponent();
            _tmr = new Timer(new TimerCallback(Tmr_TickEvent), null, Timeout.Infinite, Timeout.Infinite);
            _tmr.Change(0, 60000);// 1min. = 60000 = 60 sec.
        }
        protected override void OnStart(string[] args)
        {
            Common.WriteToFile("Service Started");
        }
        protected override void OnStop()
        {
            _tmr.Dispose();
            Common.WriteToFile("Service Stopped");
        }

        public void Tmr_TickEvent(object state)
        {
            if (timerVal == true)
            {
                timerVal = false;
                try
                {
                 
                    timerVal = true;
                }
                catch (Exception _ex)
                {
                    Common.WriteToFile(DateTime.Now + "::.......................................Error.......................................");
                    timerVal = true;
                }
            }
        }
    }
}



////////////////////////   Steps

1.    Create new project >  windows service
2.    Change service name
3.    Double click on service
4.    Right click on service design >>  Add installer
5.    Right click on ServiceInstaller1 >> property >> Change Description , DisplayName                            and  ServiceName
6.    Right click on serviceProcessInstaller1 >> property >> Account >> Select LocalSystem

Copy above code and past and debug













No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...