定時任務-C#線程類 windows服務

來源:互聯網
上載者:User

標籤:category   ima   datetime   int   hid   定義   rtb   set   時間   

原理

最常用的就是C#中 timer類寫一個定時方法,然後在把他宿主到windows服務裡面。

C#中Timer分類

關於C# Timer類  在C#裡關於定時器類就有3個

C# Timer使用的方法1.定義在System.Windows.Forms裡

C# Timer使用的方法2.定義在System.Threading.Timer類裡  "

C# Timer使用的方法3.定義在System.Timers.Timer類裡

◆System.Windows.Forms.Timer

應用於WinForm中的,它是通過Windows訊息機制實現的,類似於VB或Delphi中的Timer控制項,內部使用API  SetTimer實現的。它的主要缺點是計時不精確,而且必須有訊息迴圈,Console  Application(控制台應用程式)無法使用。   
  
◆System.Timers.Timer

和System.Threading.Timer非常類似,它們是通過.NET  Thread  Pool實現的,輕量,計時精確,對應用程式、訊息沒有特別的要求。

◆System.Timers.Timer還可以應用於WinForm,完全取代上面的Timer控制項。它們的缺點是不支援直接的拖放,需要手工編碼。

System.Threading.Timer

public class BizCommon{    /// <summary>    /// 鎖    /// </summary>    public static object LockObject = new object();    public static void StartTime()    {        //第二個參數是回調方法的參數        Timer t = new Timer(StartBiz, null, 0, 5000);        //t.Change(0, 5000);    }    private static void StartBiz(object o)    {        if (Monitor.TryEnter(LockObject))        {            FileStream fs = new FileStream("C:\\log.txt", FileMode.OpenOrCreate);            StreamWriter sw = new StreamWriter(fs);            sw.WriteLine("Time:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));            sw.Close();        }    }}
View Code

System.Timers.Timer

public class BizCommon{    /// <summary>    /// 鎖    /// </summary>    public static object LockObject = new object();    public static void StartTime()    {        System.Timers.Timer tm = new System.Timers.Timer();        tm.Interval = 5000;        tm.Elapsed += new System.Timers.ElapsedEventHandler(StartBiz);        tm.AutoReset = true; //執行一次false,一直迴圈執行true        tm.Enabled = true;//是否執行Elapsed事件。        tm.Start();        //tm.Stop();    }    private static void StartBiz(object sender, System.Timers.ElapsedEventArgs e)    {        if (Monitor.TryEnter(LockObject))        {            FileStream fs = new FileStream("C:\\log.txt", FileMode.OpenOrCreate);            StreamWriter sw = new StreamWriter(fs);            sw.WriteLine("Time:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));            sw.Close();        }    }}
View Code

更多關於多線程的教程請看

http://www.cnblogs.com/wudequn/category/1154929.html

 

public static void StartTime(){    System.Timers.Timer tm = new System.Timers.Timer();    tm.Interval = 1000;    tm.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);    tm.Start();}private static void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e){    int intHour = e.SignalTime.Hour;    int intMinute = e.SignalTime.Minute;    int intSecond = e.SignalTime.Second;    // 設定 每天 00:00:00開始執行程式      //    //由於電腦效能問題,有時候可能會跳過某一秒,導致沒有執行。  這種導致任務沒有處理的異常叫做missfire    //這種就要做很多處理,其他時間段在查看資料庫是否今天這個時間處理過。沒有處理在重新處理等等 補償操作。    if (intHour == 00 && intMinute == 00 && intSecond == 00)    {        //do something    }}
特定時間點處理任務
Windows服務

建立過程

 

下載項目

編譯完之後,裡面有安裝 和 寫在 windows服務的批次檔。

 

定時任務-C#線程類 windows服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.