2014-08-29 39 views
0

我有一種情況,我應該通過Windows服務和Timer.Here解決這個問題。我有一個winform,用戶可以在其中添加他想要的報告的時間。什麼時候他會添加將被保存到數據庫。現在,這意味着多個用戶,所以會有多個相同的數據庫中添加。現在根據我的要求,我必須發送用戶在特定的時間他必須設置我的計時器,以便每當從數據庫發出適當的時間時,應將報告發送給用戶。此後,應再次發送併發送它等等。 對於這個我使用Windows服務,並試圖在第一時間拿到上,我們需要通過執行數據庫連接和查詢設置定時器..如何在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() 
    { 
    } 
} 
} 

現在我有一個查詢如何在此添加定時器保持週期移動.. 請幫助,因爲這對我來說是合乎邏輯的路障..

回答

2

您可以創建一個定時器如下圖所示,從數據庫中收集計時器的持續時間作爲你說的它來自數據庫,每當時間過去,你可以做你的過程: -

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 
    } 
+0

我是否需要比較''計時器與系統時間,啓動過程中或會發生automatically.And我應該在哪裏廣告前三行code..In windowsservice的OnStart的( ) 方法? – user3924730 2014-08-29 10:47:02

+1

請注意,您還需要保持服務活動太 – 2014-08-29 10:47:12

+0

@ErnodeWeerd除非你正在談論'System.Threading.Timer'不需要,不要求 – 2014-08-29 10:48:55

相關問題