網站首頁 編程語言 正文
C#的timer與線程使用
卡頓怎么處理,多線程。多線程比timer好讀。看看timer和線程的關系。
timer有3種
1.winform 下的timer。就是拖控件到UI上的那個timer. ??
源文件在這個路徑下C:\Windows\Microsoft.NET\Framework64\v4.0.30319
namespace System.Windows.Forms
{
? ? // 摘要: ? ?實現按用戶定義的時間間隔引發事件的計時器。 此計時器最宜用于 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); 源碼如下。
可見,最終調用的是系統的timer?系統是有定時器的。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);
? ? ? ? ? ? });
? ? ? ? }
原文鏈接:https://blog.csdn.net/qqqgg/article/details/80234597
相關推薦
- 2022-07-11 UVM中設置打印信息的冗余度閾值和重載打印信息的嚴重性
- 2022-09-22 集合Map以及HashMap
- 2022-02-27 uniapp swiper內有點擊事件時會觸發 swiper的animationfinish事件
- 2022-05-08 一文教你向Pandas?DataFrame添加行_python
- 2022-07-31 C語言數據結構算法基礎之循環隊列示例_C 語言
- 2022-11-09 Python有序容器的?sort?方法詳解_python
- 2022-08-16 R語言繪制維恩圖ggvenn示例詳解_R語言
- 2022-05-20 jmeter連接數據庫The driver has not received any packets
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支