Reputation: 183
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..
Upvotes: 0
Views: 1734
Reputation: 11741
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
}
Upvotes: 2