經驗總結34--C#計時器,準點統計

來源:互聯網
上載者:User

標籤:c#   計時器   準點統計   

最近做一個資料統計的功能,需要準點進行統計。

分別是1分鐘,5分鐘,30分鐘,一天。

開始想著寫4個計時器分別統計,但擷取準點時間比較困難。

比如5分鐘資料,10:02分時你得統計9:55-10:00的資料,判斷太多。

最後思考下,一個1分鐘計時器就可以了,如果到5分鐘時間時同時進行1分鐘和5分鐘資料,後面依次類推。

1、計時器

System.Timers.Timer tradeTimer1 = new System.Timers.Timer();
            tradeTimer1.Interval = 1 * 60 * 1000; 
            tradeTimer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
            tradeTimer1.Enabled = true;

2、擷取準點時間,往前一分鐘靠攏

DateTime now = DateTime.Now;
                string str = now.ToShortDateString() + " " + now.ToShortTimeString();
                DateTime time = Convert.ToDateTime(str);

3、判斷統計資料

//一分鐘

//五分鐘
                        if (time.Minute == 0 || time.Minute % 5 == 0)
                        {}

 //30分鐘
                        if (time.Minute == 0 || time.Minute == 30)
                        {}

//一天
                        if (time.Minute == 0 && time.Hour == 0)
                        {}

經驗總結34--C#計時器,準點統計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.