0

I have written this working code to fire the window service at every 3 seconds interval but unable to implement for the specific system time like 12:30 AM. This code is working fine but i need the idea to implement with specific system time. Please Guide me .

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;

namespace WindowsServiceForOnlineMockTest
{
    public partial class Service1 : ServiceBase
    {
        System.Timers.Timer sysTimer = new System.Timers.Timer();
        private int _interval = 3000;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            sysTimer.Interval = _interval;
            sysTimer.Enabled = true;
            sysTimer.Start();
            sysTimer.Elapsed += new System.Timers.ElapsedEventHandler(onElapsed);
        }

        protected override void OnStop()
        {
        }


        private void onElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
            try
            {
                SqlCommand cmd = new SqlCommand("changeid", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Error", ex.Message, EventLogEntryType.Warning);
            }
        }
    }
}
Santanu Maulik
  • 111
  • 2
  • 12
  • Why don't you check in `onElapsed` if the time is 12:30 PM? – Marcel N. Aug 02 '14 at 10:29
  • An application that queries a database should not be a Windows service *anyway*. You just want a regular application that runs in the background and doesn't display any windows. – Cody Gray - on strike Aug 02 '14 at 10:29
  • @Cody are you saying that a Windows Service should never query a database? – CodeCaster Aug 02 '14 at 10:30
  • @Marcel Because running a timer that ticks at regular intervals, waking up your application just to check the time is ***horrible*** design. Any developer who does this should be taken out and shot. And any of your users that have a mobile device will do *exactly* that. – Cody Gray - on strike Aug 02 '14 at 10:30
  • 1
    Not that it is impossible, just that it doesn't look like that's what is needed here. 99.9% of the time, people who write services don't actually need or want a service. – Cody Gray - on strike Aug 02 '14 at 10:31
  • @CodyGray: OK, but I wouldn't be so drastic. I actually thought that the service does more than just that query. If not, scheduled tasks are indeed the way to go. – Marcel N. Aug 02 '14 at 10:32
  • ok ! Actually m new with Windows-services codes. – Santanu Maulik Aug 02 '14 at 10:35

0 Answers0