【问题标题】:Access to the database every 1 hour C# [closed]每 1 小时访问一次数据库 C# [关闭]
【发布时间】:2013-04-22 21:30:14
【问题描述】:

我有一个问题,我正在尝试创建一个服务来连接到我创建的 API。

这个服务要每隔一小时连接Mysql中的一个数据库,看看有没有一定的值。

例如,每次我都会查看字段 x 是否具有值 y。如果是真的,将不得不运行一些东西。

我已经阅读了一些关于 Threads 和 System.Threading.Timer 的内容,但不太明白,有人可以给我一个实际的例子或正确的方法吗,我正在寻找什么?

提前谢谢..

【问题讨论】:

    标签: c# multithreading timertask timer-jobs


    【解决方案1】:

    创建一个简单的程序来满足您的需求,并将其作为 windows task 运行,每小时运行一次。

    【讨论】:

    • 同意。在这种情况下,服务可能会过大。 Windows 任务可能是更好的方法。
    【解决方案2】:

    创建一个 Windows 服务并给它一个 1 小时的时间间隔。此 Windows 服务将始终运行,但会在指定的时间间隔触发对数据库的查询。使用 Windows 服务,您不必搞乱线程和所有内容。

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

    【讨论】:

      【解决方案3】:

      创建一个 Windows 服务并将其移动到您拥有该应用程序的服务器。此 Windows 服务将全天 24 小时运行,并会满足您的要求。

      类程序:ServiceBase { System.Timers.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)
              {
      
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-12-23
        • 2021-11-07
        • 1970-01-01
        • 2017-04-09
        • 1970-01-01
        • 1970-01-01
        • 2011-10-25
        • 2014-12-13
        • 1970-01-01
        相关资源
        最近更新 更多