0

I have a situation which i am supposed to solve by Windows service and Timer.Here is the description.I have a winform from where the user can add the time at which he wants the report.What ever time he will add will get saved into database.Now as this meant for multiple users so there will be multiple adding of the same in the database.Now as per my requirement i have to send the user the reports on specific time set by him.I have to set my timer such that whenever the appropriate time from the database comes Reports should be sent to the user.After this it should go for another time and send it and so on. For this i am using windows service and trying to get the first time on which we need to set the timer by executing database connection and query..

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 MySql.Data.MySqlClient;

namespace AutomailTrigger
{
public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    public void OnDebug()
    {

        OnStart(null);
    }


    protected override void OnStart(string[] args)
    {

        string time;

        string conn = "Server=localhost;Port=3306;Database=;UID=root;Pwd=;pooling=false";
        string Query = "SELECT * FROM `reportsetting` order by SendingTime desc limit 1;";
        MySqlConnection con = new MySqlConnection(conn);
        MySqlCommand comm = new MySqlCommand(Query, con);
        con.Open();
        MySqlDataReader dr = comm.ExecuteReader();
        while (dr.Read())
        {
            time = dr["time"].ToString();
        }

        this.timer = new System.Timers.Timer();

        // Hook up the Elapsed event for the timer.
        this.timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        this.timer.Enabled = true;


    }


    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        // Your code when the timer elapses

    }

    protected override void OnStop()
    {
    }
}
}

Now i have a query how to add timer in this to keep the cycle moving.. Please help as this is logical roadblock for me..

user3924730
  • 163
  • 1
  • 3
  • 14

1 Answers1

2

You can create a timer as shown below which collects timer elapsed time from database as you said it comes from database and whenever the time elapses you can do your process :-

var timer = new System.Timers.Timer("(Take time from database or use static time)");

// Hook up the Elapsed event for the timer.
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

timer.Enabled = true;
...

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
      // Your code when the timer elapses
}

UPDATE :-

     /// <summary>
        /// Your Timer.
        /// </summary>
        private System.Timers.Timer timer;

  protected override void OnStart(string[] args)
    {
        this.timer = new System.Timers.Timer("(Take time from database or use static time)");

        // Hook up the Elapsed event for the timer.
        this.timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        this.timer.Enabled = true;


        string conn = "Server=localhost;Port=3306;Database=;UID=root;Pwd=;pooling=false";
        string Query = "SELECT * FROM `reportsetting` order by SendingTime desc limit 1;";
        MySqlConnection con = new MySqlConnection(conn);
        MySqlCommand comm = new MySqlCommand(Query, con);
        con.Open();
        MySqlDataReader dr = comm.ExecuteReader();
        while (dr.Read())
        {

        }
    }

    protected override void OnStop()
    {
    }

    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
          // Your code when the timer elapses
    }
Neel
  • 11,625
  • 3
  • 43
  • 61
  • Do i need to compare `timer ` with the systems time to trigger the process or it will happen automatically.And where i should ad first three line of code..In windowsservice Onstart() method? – user3924730 Aug 29 '14 at 10:47
  • 1
    Note that you need to keep the service alive too – Emond Aug 29 '14 at 10:47
  • @ErnodeWeerd Not required unless you're talking about `System.Threading.Timer` – Sriram Sakthivel Aug 29 '14 at 10:48
  • @SriramSakthivel - Are you sure the timer keeps the service alive? Could you refer to some documentation on that? I am curious – Emond Aug 29 '14 at 10:53
  • @ErnodeWeerd u mean to say timer should work in background right or im getting u wrongly – Neel Aug 29 '14 at 10:56
  • @ErnodeWeerd I think I misunderstood your comment. I thought you mean to keep reference of `Timer` in instance field. Nevermind. Disregard my comment. I'm not sure about keeping the service alive. – Sriram Sakthivel Aug 29 '14 at 10:57
  • @Neel - the Elapsed handler will run on a background thread as far as I know. So if the OnStart does not start or keep a foreground thread spinning the service will stop immediately after the start. A 'simple' (non-choking) loop could be enough. – Emond Aug 29 '14 at 11:06
  • @ErnodeWeerd PLease help me sir as per your logic and syntax as i am not able to get it..Thanks.. – user3924730 Aug 29 '14 at 11:16
  • go through this http://stackoverflow.com/questions/12885013/windows-service-with-timer @user3924730 – Neel Aug 29 '14 at 11:21
  • what exactly is your problem or any error ur getting @user3924730? – Neel Aug 29 '14 at 11:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60228/discussion-between-user3924730-and-neel). – user3924730 Aug 29 '14 at 11:52
  • sorry in my company chatting is blocked so I cant open it @user3924730 – Neel Aug 29 '14 at 11:57
  • @Neel Do i need to write the database reading code in `OnTimedEvent` and one more query after the execution from `OnTimedEvent` will the control again reach to `Onstart()`.. – user3924730 Aug 29 '14 at 12:18
  • in OnTimedEvent method u need to write code which u want perform after specific time for example u said in question sending of reporst code will come there and other process work as it shouldbe @user3924730 – Neel Aug 29 '14 at 12:21
  • @Neel I have updated my post.Is it OK.Also what type of value to set in `this.timer = new System.Timers.Timer();`.Please see my updated post/// – user3924730 Aug 29 '14 at 12:27
  • in System.Timers.Timer(1000) means you want to call OnTimedEvent after 1 sec here it indicated time after which you want to perform action and it is in MilliSeconds so in ur case if you want to send report to your user every 1 minute there it would be System.Timers.Timer(60000); @user3924730 – Neel Aug 29 '14 at 12:49