欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中的timer與線程使用

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

C#的timer與線程使用

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

timer有3種

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

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

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

啟動(dòng)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();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }

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

可見,最終調(diào)用的是系統(tǒng)的timer?系統(tǒng)是有定時(shí)器的。Ucos上,就有32個(gè)定時(shí)器,當(dāng)然也可以開線程。

他們是不同的概念。windows 也差不多吧。這些定時(shí)器應(yīng)該與CPU有關(guān)。

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 隊(duì)列的操作。既然在Threading 命名空間下,可能與線程有關(guān)。他在的dll 是 mscorlib.

3.  System.Timers.Timer,  在system.dll中。  只是對(duì) 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)
? ? {
? ? ? ? // 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼
? ? ? ? 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#新線程延時(shí)

開啟一個(gè)新線程

在這個(gè)線程中,進(jìn)行任務(wù)排隊(duì)。

任務(wù)1完成后,等待延時(shí)200ms,再運(yùn)行任務(wù)2

?private void Timer1_Tick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //throw new NotImplementedException();
? ? ? ? ? ? Task.Run(() =>
? ? ? ? ? ? {
? ? ? ? ? ??
? ? ? ? ? ? ? ? this.Invoke( new Action( () =>?
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? listBox1.Items.Add("進(jìn)中斷"+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);
? ? ? ? ? ? });
? ? ? ? }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • C#實(shí)現(xiàn)數(shù)組元素的數(shù)據(jù)類型轉(zhuǎn)換方法詳解

    C#實(shí)現(xiàn)數(shù)組元素的數(shù)據(jù)類型轉(zhuǎn)換方法詳解

    這篇文章主要為大家介紹了C#中一維數(shù)組如何快速實(shí)現(xiàn)數(shù)組元素的數(shù)據(jù)類型的轉(zhuǎn)換,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-04-04
  • 利用WPF實(shí)現(xiàn)Windows屏保的制作

    利用WPF實(shí)現(xiàn)Windows屏保的制作

    屏保程序的本質(zhì)上就是一個(gè)Win32?窗口應(yīng)用程序。本文將利用WPF實(shí)現(xiàn)Windows屏保的制作,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們學(xué)習(xí)WPF有一定幫助,感興趣的可以了解一下
    2022-07-07
  • C#灰度化圖像的實(shí)例代碼

    C#灰度化圖像的實(shí)例代碼

    灰度化一幅圖像就是將圖像的色彩信息全部丟掉,將24位的位圖信息,用8位來(lái)表示,灰度圖共有256級(jí)灰度等級(jí),也就是將24位位圖的一點(diǎn)如(255,255,255)轉(zhuǎn)換成255,所以R,G,B三個(gè)值所乘的系數(shù)和為1
    2013-09-09
  • C# 泛型編譯特性對(duì)性能的影響小結(jié)

    C# 泛型編譯特性對(duì)性能的影響小結(jié)

    C#作為一種強(qiáng)類型語(yǔ)言,具有豐富的泛型支持,允許開發(fā)者編寫可以應(yīng)對(duì)不同數(shù)據(jù)類型的通用代碼,這篇文章主要介紹了C# 泛型編譯特性對(duì)性能的影響 ,需要的朋友可以參考下
    2023-11-11
  • C#中volatile與lock用法

    C#中volatile與lock用法

    這篇文章主要介紹了C#中volatile與lock用法,較為詳細(xì)的分析了C#中volatile與lock的適用情況及用法實(shí)例,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C#線程倒計(jì)時(shí)器源碼分享

    C#線程倒計(jì)時(shí)器源碼分享

    這篇文章主要為大家分享了C#線程倒計(jì)時(shí)器源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C#字符串與數(shù)值類型、字節(jié)數(shù)組的互相轉(zhuǎn)換實(shí)戰(zhàn)案例

    C#字符串與數(shù)值類型、字節(jié)數(shù)組的互相轉(zhuǎn)換實(shí)戰(zhàn)案例

    最近由于編程的需要,對(duì)C#的類型轉(zhuǎn)換做了一些研究,下面這篇文章主要給大家介紹了關(guān)于C#字符串與數(shù)值類型、字節(jié)數(shù)組的互相轉(zhuǎn)換的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • Unity之繞軸進(jìn)行旋轉(zhuǎn)的操作

    Unity之繞軸進(jìn)行旋轉(zhuǎn)的操作

    這篇文章主要介紹了Unity之繞軸進(jìn)行旋轉(zhuǎn)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2021-04-04
  • C#常用目錄文件操作類實(shí)例

    C#常用目錄文件操作類實(shí)例

    這篇文章主要介紹了C#常用目錄文件操作類,實(shí)例分析了C#針對(duì)目錄的讀取、檢測(cè)及查找等相關(guān)操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • C#線程 BeginInvoke和EndInvoke使用方法

    C#線程 BeginInvoke和EndInvoke使用方法

    本文開始C#線程系列講座之一,即BeginInvoke和EndInvoke的使用方法,需要的朋友可以參考下
    2013-05-05

最新評(píng)論