System.Timers.Timer與System.Windows.Forms.Timer 區別

來源:互聯網
上載者:User
           根據msdn解釋:System.Threading.Timer 是一個簡單的輕量計時器,它使用回調方法並由線程池線程提供服務。
不建議將其用於 Windows 表單,因為其回調不在使用介面執行緒上進行。
System.Windows.Forms.Timer 是用於 Windows 表單的更佳選擇。
Windows 表單 Timer 組件是單執行緒元件,精度限定為 55 毫秒。
如果您需要更高精度的多線程計時器,請使用System.Threading 命名空間中的
Timer 類。要擷取基於伺服器的計時器功能,可以考慮使用
System.Threading.Timer,它可以引發事件並具有其他功能。

          簡而言之,System.Threading.Timers命名空間中的Timer類主要是針對多線程情況下使用的。而System.Windows.Forms.Timer中的Timer主要是單線程的,即主要是針對某個表單使用的。舉個例子,比如主表單中可以放一個System.Windows.Forms.Timer動態顯示目前時間,每秒更新一次時間顯示在右下角.

  private void timer1_Tick(object sender, EventArgs e)
        {
            lblTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
            lblTime.ForeColor = Color.FromArgb(0x2b, 0x47, 0x5b);
        }

而像主表單中的Socket的通訊,則要單獨開一個線程處理。例如在發送心調包時就可以用到
System.Threading.Timer來定時發送心跳包了,此時System.Threading.Timer就只在監控心跳包的這個線程上。下面是範例程式碼:

    /// <summary>
    /// 監聽Socket的串連狀態
    /// 如果Socket串連斷開,則進行重新串連
    /// 如果Socket為串連狀態,則發送狀態確認包
    /// </summary>
    private void ListenSocCon()
    {
        int interval = 0;
        if (ConfigurationManager.AppSettings["ListenSocTime"] != null)
            {
            int i = 0;
            if (int.TryParse(ConfigurationManager.AppSettings["ListenSocTime"].ToString(), out i))
                {
                interval = 1000 * i;
                }
            else
                {
                interval = 10000;
                }
            }
            //記下日誌
            string strOuput = string.Format("開啟監聽Socket連接線程,時間間隔為:{0}\n",interval.ToString());
            //將資訊寫入到日誌輸出檔案
            DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
            try
            {

                //使用TimerCallback
委託指定希望 Timer 執行的方法
                TimerCallback timerDelegate = new TimerCallback(tm_ConSock);
                timerConSocket = new System.Threading.Timer(timerDelegate, this, 0, interval);
            }
            catch (Exception e)
            {
                strOuput = string.Format("監聽Socket的串連狀態出現錯誤:{0}\n", e.Message);
                //將資訊寫入到日誌輸出檔案
                DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
            }
        }

    要想更詳細的瞭解System.Threading.Timer和 
System.Windows.Forms.Timer請參考MSDN:

  http://msdn.microsoft.com/zh-cn/library/system.threading.timer.aspx;

http://msdn.microsoft.com/zh-cn/library/system.windows.forms.timer.aspx。

相關文章

聯繫我們

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