如何在windows服务中添加计时器

本文关键字:添加 计时器 服务 windows | 更新日期: 2023-09-27 18:19:54

我有一个情况,我应该通过Windows服务和Timer来解决。下面是描述。我有一个winform,用户可以从中添加他想要报告的时间。他每次添加的内容都会被保存到数据库中。现在,因为这意味着要为多个用户添加,所以数据库中会多次添加相同的内容。现在,根据我的要求,我必须向用户发送他设定的特定时间的报告。我必须设置我的计时器,以便每当数据库中的适当时间到来时,都应该向用户发送报告。在这之后,它应该去另一个时间,并发送它,等等。为此,我使用windows服务,并试图通过执行数据库连接和查询来获得第一次需要设置计时器的时间。。

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()
    {
    }
}
}

现在我有一个问题,如何在其中添加计时器来保持循环。。请帮忙,因为这是我的逻辑障碍。

如何在windows服务中添加计时器

您可以创建一个计时器,如下所示,它从数据库中收集计时器运行的时间,正如您所说的,它来自数据库,无论何时,您都可以执行您的过程:-

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
}

更新:-

     /// <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
    }