Advanced_C#_Skill_Multithreading with timer

今天我要來示範練習寫一個

關於C#  Timer 多執行緒 的寫法


我們產生一個 Console Application




第一步驟 . 引入 using System.Threading





第二步驟. 寫一個 call by Thread 的 function




第三步驟. 為 Threading with timer 寫 code


System.Threading.Timer threadingTimer 
new Timer(TimerCallback callback , //TimerCallback回呼
                   object state , //包含回呼方法中,所使用的資訊的物件或 null
                   int due time,//定時器啟用後多久時間進行第一次呼叫
                   int period);//間隔多久時間進行下一次呼叫



link: https://msdn.microsoft.com/zh-tw/library/1cy0c7zw.aspx








每隔3秒就讓執行續   執行一次 印出目前時間的字串


程式碼


static void Main(string[] args)
        {
            //define thread with timer
            System.Threading.Timer threadingTimer = new Timer(run, 10, 1, 3000);
            //每隔3000 millisecond 也就是 3秒 thread 就excute 一次
            Console.ReadKey();//卡住
        }
        // function define for thread
        static void run(object args)
        {
            Console.WriteLine("Time :" + DateTime.Now.ToString());
        }





學自:

Multithreading with timer in c# windows application or Desktop application

https://www.youtube.com/watch?v=9kFpkDvhvEY




留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題