最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中的timer與線程使用

 更新時間:2022年08月12日 11:18:30   作者:qgbooooo  
這篇文章主要介紹了C#中的timer與線程使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

C#的timer與線程使用

卡頓怎么處理,多線程。多線程比timer好讀??纯磘imer和線程的關系。

timer有3種

1.winform 下的timer。就是拖控件到UI上的那個timer.   

源文件在這個路徑下C:\Windows\Microsoft.NET\Framework64\v4.0.30319

namespace System.Windows.Forms
{
? ? // 摘要: ? ?實現(xiàn)按用戶定義的時間間隔引發(fā)事件的計時器。 此計時器最宜用于 Windows 窗體應用程序中,并且必須在窗口中 ? ? 使用。
? ? [DefaultEvent("Tick")]
? ? [DefaultProperty("Interval")][SRDescriptionAttribute("DescriptionTimer")][ToolboxItemFilter("System.Windows.Forms")]
? ? public class Timer : Component
}

啟動timer代碼如下:  

[SRCategory("CatBehavior")]
? ? [DefaultValue(false)]
? ? [SRDescription("TimerEnabledDescr")]
? ? public virtual bool Enabled
? ? {
? ? ? ? get
? ? ? ? {
? ? ? ? ? ? if (this.timerWindow == null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return this.enabled;
? ? ? ? ? ? }
? ? ? ? ? ? return this.timerWindow.IsTimerRunning;
? ? ? ? }
? ? ? ? set
? ? ? ? {
? ? ? ? ? ? lock (this.syncObj)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (this.enabled != value)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? this.enabled = value;
? ? ? ? ? ? ? ? ? ? if (!base.DesignMode)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (value)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (this.timerWindow == null)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? this.timerWindow = new TimerNativeWindow(this);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.timerRoot = GCHandle.Alloc(this);
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.timerWindow.StartTimer(this.interval);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (this.timerWindow != null)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? this.timerWindow.StopTimer();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (this.timerRoot.IsAllocated)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? this.timerRoot.Free();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }

最終調用了this.timerWindow.StartTimer(this.interval); 源碼如下。

可見,最終調用的是系統(tǒng)的timer?系統(tǒng)是有定時器的。Ucos上,就有32個定時器,當然也可以開線程。

他們是不同的概念。windows 也差不多吧。這些定時器應該與CPU有關。

public void StartTimer(int interval)
? ? {
? ? ? ? if (this._timerID == 0 && !this._stoppingTimer && this.EnsureHandle())
? ? ? ? {
? ? ? ? ? ? this._timerID = (int)SafeNativeMethods.SetTimer(new HandleRef(this, base.Handle), TimerNativeWindow.TimerID++, interval, IntPtr.Zero);
? ? ? ? }
? ? }

2.  public sealed class Timer : MarshalByRefObject, IDisposable   System.Threading.Timer

?? ?public Timer(TimerCallback callback)
?? ?{
?? ??? ?int dueTime = -1;
?? ??? ?int period = -1;
?? ??? ?StackCrawlMark stackCrawlMark = StackCrawlMark.LookForMyCaller;
?? ??? ?this.TimerSetup(callback, this, (uint)dueTime, (uint)period, ref stackCrawlMark);
?? ?}
?
?? ?[SecurityCritical]
?? ?private void TimerSetup(TimerCallback callback, object state, uint dueTime, uint period, ref StackCrawlMark stackMark)
?? ?{
?? ??? ?if (callback == null)
?? ??? ?{
?? ??? ??? ?throw new ArgumentNullException("TimerCallback");
?? ??? ?}
?? ??? ?this.m_timer = new TimerHolder(new TimerQueueTimer(callback, state, dueTime, period, ref stackMark));
?? ?}
?
?? ?[SecurityCritical]
?? ?internal static void Pause()
?? ?{
?? ??? ?TimerQueue.Instance.Pause();
?? ?}
?
?? ?[SecurityCritical]
?? ?internal static void Resume()
?? ?{
?? ??? ?TimerQueue.Instance.Resume();
?? ?}

這里是TimerQueue 隊列的操作。既然在Threading 命名空間下,可能與線程有關。他在的dll 是 mscorlib.

3.  System.Timers.Timer,  在system.dll中。  只是對 System.Threading.Timer的封裝。

?? ?[TimersDescription("TimerEnabled")]
?? ?[DefaultValue(false)]
?? ?public bool Enabled
?? ?{
?? ??? ?get
?? ??? ?{
?? ??? ??? ?return this.enabled;
?? ??? ?}
?? ??? ?set
?? ??? ?{
?? ??? ??? ?if (base.DesignMode)
?? ??? ??? ?{
?? ??? ??? ??? ?this.delayedEnable = value;
?? ??? ??? ??? ?this.enabled = value;
?? ??? ??? ?}
?? ??? ??? ?else if (this.initializing)
?? ??? ??? ?{
?? ??? ??? ??? ?this.delayedEnable = value;
?? ??? ??? ?}
?? ??? ??? ?else if (this.enabled != value)
?? ??? ??? ?{
?? ??? ??? ??? ?if (!value)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (this.timer != null)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?this.cookie = null;
?? ??? ??? ??? ??? ??? ?this.timer.Dispose();
?? ??? ??? ??? ??? ??? ?this.timer = null;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?this.enabled = value;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?this.enabled = value;
?? ??? ??? ??? ??? ?if (this.timer == null)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?if (this.disposed)
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?throw new ObjectDisposedException(base.GetType().Name);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?int num = (int)Math.Ceiling(this.interval);
?? ??? ??? ??? ??? ??? ?this.cookie = new object();
?? ??? ??? ??? ??? ??? ?this.timer = new System.Threading.Timer(this.callback, this.cookie, num, this.autoReset ? num : (-1));
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?this.UpdateTimer();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}

4.使用:

void Application_Start(object sender, EventArgs e)
? ? {
? ? ? ? // 在應用程序啟動時運行的代碼
? ? ? ? if (timer != null)
? ? ? ? {
? ? ? ? ? ? timer.Stop();
? ? ? ? ? ? timer.Close();
? ? ? ? ? ? timer = null;
? ? ? ? }
? ? ? ? int Interval = 3600000;//6 hours?
? ? ? ? timer = new System.Timers.Timer(Interval);//十分鐘 ?
? ? ? ? timer.Elapsed += SendSMS.Send_ticker;
? ? ? ? timer.Interval = Interval;
? ? ? ? timer.Enabled = true;
? ? ? ? timer.Start();
? ? }

C#新線程延時

開啟一個新線程

在這個線程中,進行任務排隊。

任務1完成后,等待延時200ms,再運行任務2

?private void Timer1_Tick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //throw new NotImplementedException();
? ? ? ? ? ? Task.Run(() =>
? ? ? ? ? ? {
? ? ? ? ? ??
? ? ? ? ? ? ? ? this.Invoke( new Action( () =>?
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? listBox1.Items.Add("進中斷"+DateTime.Now.ToString() + "\r\n");
? ? ? ? ? ? ? ? }));
? ? ? ? ? ? ? ? //RS485.Set_io(7);//ok
? ? ? ? ? ? ? ? //RS485.Rest_io(7);//ok
? ? ? ? ? ? ? ? if (i > 8) i = 0;
? ? ? ? ? ? ? ? RS485.Set_io(i++);//ok
? ? ? ? ? ? ? ? this.Invoke(new Action(() =>
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? listBox1.Items.Add("第1次輸出" + DateTime.Now.ToString() + "\r\n");
? ? ? ? ? ? ? ? }));
?
?
? ? ? ? ? ? ? ? Thread.Sleep(200);
? ? ? ? ? ? ? ? RS485.Rest_io((ushort)(i - 2));//ok ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? this.Invoke(new Action(() =>
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? listBox1.Items.Add("第2次輸出" + DateTime.Now.ToString() + "\r\n");
? ? ? ? ? ? ? ? }));
?
?
? ? ? ? ? ? ? ? Thread.Sleep(200);
? ? ? ? ? ? ? ? //RS485.Read_io_out(0,8);//ok
? ? ? ? ? ? ? ? RS485.Read_io_in(0, 8);//ok
? ? ? ? ? ? ? ? this.Invoke(new Action(() =>
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? listBox1.Items.Add("第3次輸出" + DateTime.Now.ToString() + "\r\n");
? ? ? ? ? ? ? ? }));
? ? ? ? ? ? ? ? //RS485.Read_io_Reg(0,4);//
? ? ? ? ? ? ? ? //RS485.Read_io_Regs(0, 6);//
? ? ? ? ? ? ? ? Thread.Sleep(200);
? ? ? ? ? ? });
? ? ? ? }

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關文章

  • C#中Lambda表達式的用法

    C#中Lambda表達式的用法

    這篇文章介紹了C#中Lambda表達式的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#并行庫Parallel類介紹

    C#并行庫Parallel類介紹

    這篇文章介紹了C#并行庫Parallel類,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • c#制作類似qq安裝程序一樣的單文件程序安裝包

    c#制作類似qq安裝程序一樣的單文件程序安裝包

    c#制作單文件安裝程序,可安裝windows服務,類似安裝QQ,大家參考使用吧
    2014-01-01
  • Unity3D實現(xiàn)NavMesh導航網格尋路

    Unity3D實現(xiàn)NavMesh導航網格尋路

    這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)NavMesh導航網格尋路,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • C#異步的世界(下)

    C#異步的世界(下)

    這篇文章主要介紹了C#異步的世界(下),對異步感興趣的同學,可以參考下
    2021-04-04
  • c#使用wmi查詢usb設備信息示例

    c#使用wmi查詢usb設備信息示例

    這篇文章主要介紹了c#使用wmi查詢usb設備信息示例,大家參考使用吧
    2014-01-01
  • C#語音識別用法實例

    C#語音識別用法實例

    這篇文章主要介紹了C#語音識別用法,實例分析了C#利用微軟操作系統(tǒng)自動的語音識別功能,讀取信息的技巧,需要的朋友可以參考下
    2015-01-01
  • C#基于Mongo的官方驅動手擼一個Super簡易版MongoDB-ORM框架

    C#基于Mongo的官方驅動手擼一個Super簡易版MongoDB-ORM框架

    本文給大家分享C#基于Mongo的官方驅動手擼一個簡易版MongoDB-ORM框架,是一款屬于super簡易版的,通過圖文的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-05-05
  • C#利用OLEDB實現(xiàn)將DataTable寫入Excel文件中

    C#利用OLEDB實現(xiàn)將DataTable寫入Excel文件中

    這篇文章主要為大家詳細介紹了C#如何利用OLEDB實現(xiàn)將DataTable寫入Excel文件中,文中的示例代碼簡潔易懂,具有一定的借鑒價值,需要的可以參考一下
    2023-02-02
  • C#利用反射技術實現(xiàn)去掉按鈕選中時的邊框效果

    C#利用反射技術實現(xiàn)去掉按鈕選中時的邊框效果

    這篇文章主要介紹了C#利用反射技術實現(xiàn)去掉按鈕選中時的邊框效果,涉及C#針對窗口的參數設置技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09

最新評論

静海县| 五常市| 化德县| 郧西县| 墨玉县| 保康县| 张家港市| 临沂市| 瑞昌市| 同德县| 湖州市| 平泉县| 嘉荫县| 新化县| 剑川县| 鄂伦春自治旗| 西贡区| 景宁| 梓潼县| 山阳县| 青田县| 阿荣旗| 启东市| 清丰县| 南汇区| 福州市| 衡水市| 阜宁县| 朝阳区| 和政县| 昭苏县| 白朗县| 淄博市| 财经| 东源县| 林州市| 育儿| 镇远县| 临清市| 宕昌县| 景宁|