Question

I have a problem, I'm trying to create a service to connect to an API that I have created.

This service has to connect to a database in Mysql every hour, and see if there is a certain value.

For example every time I'll see if the field x has the value y. If is true, will have to run something.

I already read some things about Threads and System.Threading.Timer, but do not quite understand, can someone give me practical example or the right way to do it, of what I'm looking for, please?

Thanks in advance ..

Was it helpful?

Solution

Create a simple program that does what you need and run it as a windows task which runs each hour.

OTHER TIPS

Create an windows service and give it a time interval of 1 hr. This windows service will always be running but will fire the query to database at the assigned interval. with windows service you dont have to mess with threads and all.

partial class YourService : ServiceBase
{
    Timer timer = new Timer();
    ...
    ...


    public YourService()
    { 
        InitializeComponent();
    }

    /// <summary>
    /// 

    protected override void OnStart(string[] args)
    {
        timer.Interval = 1000000; /*The interval of the Windows Service Cycle set this to one hour*/
        timer.Start();
        timer.Enabled = true;
        timer.AutoReset = true;
        timer.Elapsed += new ElapsedEventHandler(OnElapseTime); /*fire the event after each cycle*/
    }

    private void OnElapseTime(object sender, ElapsedEventArgs e)
    {
         // HERE DO UR DATABASE QUERY AND ALL
    }

    ...
    ...
}

Create a windows service and move it to the server that you are having the application. This windows service will run 24 hours a day and will do your requirement.

class Program : ServiceBase { System.Timers.Timer timer;

    static void Main(string[] args)
    {
        ServiceBase.Run(new Program());
    }
    public Program()
    {
        this.ServiceName = "DB Sync";
    }
    protected override void OnStart(string[] args)
    {
        base.OnStart(args);
        InitializeTimer();

    }

    protected override void OnStop()
    {
        base.OnStop();

    }

    protected void InitializeTimer()
    {
        try
        {
            if (timer == null)
            {
                timer = new System.Timers.Timer();
                timer.Enabled = true;
                timer.AutoReset = true;
                timer.Interval = 60000 * 1;
                timer.Enabled = true;
                timer.Elapsed += timer_Elapsed;
            }

        }
        catch (Exception ex)
        {

        }
        finally
        {
        }
    }

    protected void timer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
    {

        TimerTick();
        timer.Interval = 60000 * Convert.ToDouble(ConfigurationManager.AppSettings["TimerInerval"]);
    }

    private void TimerTick()
    {
        try
        {
           // Query the DB in this place.
        }
        catch (Exception ex)
        {

        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top