{"id":410,"date":"2023-12-01T22:45:46","date_gmt":"2023-12-01T14:45:46","guid":{"rendered":"https:\/\/www.n61.net\/?p=410"},"modified":"2023-12-01T22:45:46","modified_gmt":"2023-12-01T14:45:46","slug":"dotnet-timer","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=410","title":{"rendered":".NET\u4e2d\u6709\u591a\u5c11\u79cd\u5b9a\u65f6\u5668"},"content":{"rendered":"<p>.NET\u4e2d\u81f3\u5c11\u67096\u79cd\u5b9a\u65f6\u5668\uff0c\u6bcf\u4e00\u79cd\u5b9a\u65f6\u5668\u90fd\u6709\u5b83\u7684\u7528\u9014\u548c\u7279\u70b9\u3002\u6839\u636e\u5b9a\u65f6\u5668\u7684\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5206\u4e3aUI\u76f8\u5173\u7684\u5b9a\u65f6\u5668\u548cUI\u65e0\u5173\u7684\u5b9a\u65f6\u5668\u3002\u672c\u6587\u5c06\u7b80\u5355\u4ecb\u7ecd\u8fd96\u79cd\u5b9a\u65f6\u5668\u7684\u57fa\u672c\u7528\u6cd5\u548c\u7279\u70b9\u3002<\/p>\n<p>UI\u5b9a\u65f6\u5668#<br \/>\n.NET\u4e2d\u7684UI\u5b9a\u65f6\u5668\u4e3b\u8981\u662fWinForm\u3001WPF\u4ee5\u53caWebForm\u4e2d\u7684\u5b9a\u65f6\u5668\u3002\u5206\u522b\u4e3a\uff1a<\/p>\n<p>System.Windows.Forms.Timer<br \/>\nSystem.Windows.Threading.DispatcherTimer<br \/>\nSystem.Web.UI.Timer<\/p>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cWinForm\u3001WPF\u4e2d\u7684\u5b9a\u65f6\u5668\u662f\u5728UI\u7ebf\u7a0b\u4e0a\u6267\u884c\u56de\u8c03\u51fd\u6570\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95eeUI\u5143\u7d20\u3002\u7531\u4e8eWinForm\u3001WPF\u652f\u6301\u5355\u7ebf\u7a0b\u5355\u5143\u6a21\u578b\uff08Single-Thread Apartment\uff0cSTA\uff09\uff0c\u5b9a\u65f6\u5668\u95f4\u9694\u4e8b\u4ef6\u662f\u5728UI\u7ebf\u7a0b\u4e0a\u89e6\u53d1\uff0c\u56e0\u6b64\uff0c\u4e0d\u7528\u62c5\u5fc3\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<br \/>\nSystem.Web.UI.Timer\u662f\u901a\u8fc7Javascript\u5b9a\u65f6\u5668\u548c\u670d\u52a1\u7aef\u5f02\u6b65\u56de\u8c03\u5b9e\u73b0\uff0c\u4e5f\u662f\u5355\u7ebf\u7a0b\u7684\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u8fd9\u91cc\u8bf4\u7684\u662f\u901a\u5e38\u60c5\u51b5\uff0c\u540e\u8fb9\u4ecb\u7ecdSystem.Windows.Threading.DispatcherTimer\u65f6\u4f1a\u63d0\u5230\u5728\u975eUI\u7ebf\u7a0b\u521b\u5efaDispatcherTimer\u65f6\u4e5f\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95eeUI\u5143\u7d20\u3002<\/p>\n<h2>System.Windows.Forms.Timer<\/h2>\n<p>System.Windows.Forms.Timer\u9488\u5bf9WinForm\u5e94\u7528\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u662f\u53ea\u80fd\u5728WinForm\u4e0a\u4f7f\u7528\u7684\u5b9a\u65f6\u5668\u3002\u8fd9\u4e2a\u5b9a\u65f6\u5668\u662f\u9488\u5bf9\u5355\u7ebf\u7a0b\u73af\u5883\u8bbe\u8ba1\u7684\uff0c\u662f\u5728UI\u7ebf\u7a0b\u4e0a\u5904\u7406\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<p>\u5b83\u8981\u6c42\u7528\u6237\u4ee3\u7801\u6709\u53ef\u7528\u7684UI\u6d88\u606f\u6cf5\uff0c\u5b9a\u65f6\u4efb\u52a1\u987b\u5728UI\u7ebf\u7a0b\u4e0a\u8fd0\u884c\uff0c\u6216\u8005\u8de8\u7ebf\u7a0b\u901a\u8fc7Invoke\u6216\u8005BeginInvoke\u5c01\u9001(marshal)\u5230UI\u7ebf\u7a0b\u4e0a\u8fd0\u884c\u3002\u5176\u4f18\u70b9\u662f\u4f7f\u7528\u7b80\u5355\uff0c\u53ea\u9700\u901a\u8fc7\u7ed9Interval\u5c5e\u6027\u8d4b\u503c\u6765\u8bbe\u7f6e\u65f6\u95f4\u95f4\u9694\uff0c\u5e76\u6ce8\u518cTick\u4e8b\u4ef6\u5904\u7406\u5b9a\u65f6\u4efb\u52a1\u3002\u5176\u7f3a\u70b9\u662f\u7cbe\u5ea6\u4e0d\u9ad8\uff0c\u7cbe\u5ea6\u4e3a55\u6beb\u79d2\uff0c\u4e5f\u5c31\u662fInterval\u8d4b\u503c\u5c0f\u4e8e55\u65f6\uff0c\u4e5f\u662f55\u6beb\u79d2\u89e6\u53d1\u4e00\u6b21\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic partial class TimerFrom : Form\n{\n    private System.Windows.Forms.Timer digitalClock;\n    private void TimerFrom_Load(object sender, EventArgs e)\n    {\n        digitalClock = new System.Windows.Forms.Timer();\/\/\u521b\u5efa\u5b9a\u65f6\u5668 \n        digitalClock.Tick += new EventHandler(HandleTime);\/\/\u6ce8\u518c\u5b9a\u65f6\u4efb\u52a1\u4e8b\u4ef6 \n        digitalClock.Interval = 1000;\/\/\u8bbe\u7f6e\u65f6\u95f4\u95f4\u9694\n        digitalClock.Enabled = true;\n        digitalClock.Start(); \/\/\u5f00\u542f\u5b9a\u65f6\u5668\n    }\n    public void HandleTime(Object myObject, EventArgs myEventArgs)\n    {\n        labelClock.Text = DateTime.Now.ToString(&quot;yyyy-MM-dd HH:mm:ss&quot;);\n    }\n    private void frmTimerDemo_FormClosed(object sender, FormClosedEventArgs e)\n    {\n        digitalClock.Stop();\/\/\u505c\u6b62\u5b9a\u65f6\u5668\n        digitalClock.Dispose();\n    }\n}\n<\/pre>\n<\/p>\n<h2>System.Windows.Threading.DispatcherTimer<\/h2>\n<p>System.Windows.Threading.DispatcherTimer\u662fWPF\u4e2d\u7684\u5b9a\u65f6\u5668\uff0c\u5b83\u662f\u57fa\u4e8eDispatcher\u5bf9\u8c61\u7684(\u5e76\u4e0d\u662f\u57fa\u4e8eUI\u7ebf\u7a0b\u7684)\u3002DispatcherTimer\u7684\u5b9a\u65f6\u4efb\u52a1\u662f\u50cf\u5176\u4ed6\u64cd\u4f5c\u4e00\u6837\u653e\u5728Dispatcher\u961f\u5217\u4e0a\uff0c\u5176\u6267\u884c\u64cd\u4f5c\u65f6\u95f4\u4f9d\u8d56\u4e8e\u961f\u5217\u4e2d\u5176\u4ed6\u4efb\u52a1\u53ca\u5176\u4f18\u5148\u7ea7\uff0c\u56e0\u6b64\uff0cDispatcherTimer\u4e0d\u4fdd\u8bc1\u5728\u65f6\u95f4\u95f4\u9694\u53d1\u751f\u65f6\u51c6\u786e\u6267\u884c\uff0c\u53ea\u4fdd\u8bc1\u4e0d\u4f1a\u5728\u65f6\u95f4\u95f4\u9694\u53d1\u751f\u524d\u6267\u884c\u3002<\/p>\n<p>Dispatcher\u4e3a\u7279\u5b9a\u7ebf\u7a0b\u7ef4\u62a4\u5de5\u4f5c\u9879\uff08\u64cd\u4f5c\uff09\u7684\u4f18\u5148\u7ea7\u961f\u5217\uff0c\u5728\u7ebf\u7a0b\u4e0a\u521b\u5efaDispatcher\u5bf9\u8c61\u65f6\uff0c\u5b83\u6210\u4e3a\u552f\u4e00\u53ef\u4ee5\u5173\u8054\u8be5\u7ebf\u7a0b\u7684Dispatcher\u5bf9\u8c61\uff0cWPF\u4e2d\uff0cDispatcherObject\u53ea\u80fd\u88ab\u4e0e\u4e4b\u5173\u8054\u7684Dispatcher\u5bf9\u8c61\u8bbf\u95ee\uff0c\u4e5f\u5c31\u662f\u975eUI\u7ebf\u7a0b\u4e2d\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95eeUI\u5143\u7d20\uff08WPF\u4e2d\u7684UI\u5143\u7d20\u90fd\u662f\u6d3e\u751f\u81eaDispatcherObject\uff09<\/p>\n<p>\u6b64\u5916\uff0cDispatcherTimer\u4e0d\u50cfSystem.Windows.Forms.Timer\u90a3\u6837\u53ea\u5728UI\u7ebf\u7a0b\u4e0a\u521b\u5efa\u624d\u80fd\u89e6\u53d1Tick\u4e8b\u4ef6\uff0c\u5b83\u5728\u975eUI\u7ebf\u7a0b\u4e0b\u521b\u5efa\u4e5f\u53ef\u4ee5\u89e6\u53d1Tick\u4e8b\u4ef6\uff0c\u6b64\u65f6\u8bbf\u95eeUI\u5143\u7d20\u4e5f\u9700\u8981\u901a\u8fc7Invoke\u6216\u8005BeginInvoke\u5c01\u9001(marshal)\u5230UI\u7ebf\u7a0b\u4e0a\u8fd0\u884c\u3002\u5176\u4f18\u70b9\u4e5f\u662f\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5728UI\u7ebf\u7a0b\u4e0a\u6267\u884c\u4efb\u52a1\u6216\u89e6\u53d1\u4e8b\u4ef6\uff0c\u7f3a\u70b9\u662f\u7cbe\u5ea6\u4e0d\u51c6\u786e\uff0c\u53ef\u80fd\u5b58\u5728\u5ef6\u8fdf\u3002<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nprivate void Dt_Tick(object sender, EventArgs e)\n{\n    Dispatcher.BeginInvoke((Action)delegate ()\n    {\n        text1.Text = DateTime.Now.ToString();\n    });\n    Console.WriteLine(DateTime.Now.ToString());\n}\n\nprivate void Button_Click(object sender, RoutedEventArgs e)\n{\n    Task.Run(() =&gt;{\n        DispatcherTimer dt = new DispatcherTimer();\n        dt.Tick += Dt_Tick;\n        dt.Interval = TimeSpan.FromSeconds(1);\n        dt.Start();\n        Dispatcher.Run();\n    });\n}\n<\/pre>\n<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cDispatcherTimer\u662f\u975eUI\u7ebf\u7a0b\u4e2d\u521b\u5efa\uff0c\u5b9a\u65f6\u4efb\u52a1\u4e2d\u8bbf\u95eeUI\u5143\u7d20text1\uff0c\u9700\u8981\u901a\u8fc7Invoke\u6216\u8005BeginInvoke\u5c01\u9001(marshal)\u5230UI\u7ebf\u7a0b\u4e0a\u8fd0\u884c\uff0c\u800cConsole.WriteLine\u5219\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u3002<\/p>\n<h2>System.Web.UI.Timer<\/h2>\n<p>System.Web.UI.Timer\u662f\u4ec5\u9002\u7528\u4e8e.NET Framework\u7684ASP.NET\u7ec4\u4ef6\u3002\u901a\u8fc7Javascript\u5b9a\u65f6\u5668\u548c\u670d\u52a1\u7aef\u5f02\u6b65\u56de\u8c03\u5b9e\u73b0\u3002\u6bcf\u6b21\u89e6\u53d1\u5b9a\u65f6\u5668\u65f6\uff0c\u53ea\u80fd\u6267\u884c\u4e00\u4e2a\u5f02\u6b65\u56de\u8c03\u65b9\u6cd5\uff0c\u800c\u5176\u4ed6\u7684\u5f02\u6b65\u56de\u8c03\u65b9\u6cd5\u9700\u8981\u7b49\u5f85\u524d\u4e00\u4e2a\u5f02\u6b65\u56de\u8c03\u65b9\u6cd5\u6267\u884c\u5b8c\u6bd5\u540e\u624d\u80fd\u6267\u884c\u3002\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u5728\u4efb\u610f\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u5f02\u6b65\u56de\u8c03\u65b9\u6cd5\u5728\u6267\u884c\uff0c\u907f\u514d\u4e86\u591a\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c\u7684\u95ee\u9898\u3002<\/p>\n<h2>UI\u65e0\u5173\u5b9a\u65f6\u5668<\/h2>\n<p>\u4ece .NET 6\u5f00\u59cb\uff0cUI\u65e0\u5173\u5b9a\u65f6\u5668\u6709\u4e09\u4e2a\uff1a<\/p>\n<p>System.Threading.Timer<br \/>\nSystem.Timers.Timer<br \/>\nSystem.Threading.PeriodicTimer\uff08.NET 6+\uff09<\/p>\n<h3>System.Threading.Timer<\/h3>\n<p>System.Threading.Timer\u662f\u6700\u57fa\u7840\u8f7b\u91cf\u7684\u5b9a\u65f6\u5668\uff0c\u5b83\u5c06\u5b9a\u671f\u5728\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u4e0a\u6267\u884c\u5355\u4e2a\u56de\u8c03\u65b9\u6cd5\u3002\u5728\u521b\u5efa\u5b9a\u65f6\u5668\u5bf9\u8c61\u65f6\u5fc5\u987b\u6307\u5b9a\u56de\u8c03\u65b9\u6cd5\uff0c\u5e76\u4e14\u540e\u7eed\u4e0d\u80fd\u4fee\u6539\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u6307\u5b9a\u5b9a\u65f6\u5668\u56de\u8c03\u5f00\u59cb\u6267\u884c\u7684\u65f6\u95f4\u4ee5\u53ca\u65f6\u95f4\u95f4\u9694\u3002\u5b9a\u65f6\u5668\u521b\u5efa\u540e\u53ef\u4ee5\u901a\u8fc7Change\u65b9\u6cd5\u4fee\u6539\u56de\u8c03\u5f00\u59cb\u6267\u884c\u7684\u65f6\u95f4\u4ee5\u53ca\u65f6\u95f4\u95f4\u9694\u3002\u8be5\u5b9a\u65f6\u5668\u7684\u4f18\u70b9\u662f\u8f7b\u91cf\uff0c\u7cbe\u5ea6\u76f8\u5bf9\u8f83\u9ad8\uff0c\u4e0eWindows\u64cd\u4f5c\u7cfb\u7edf\u65f6\u949f\u7cbe\u5ea6\u4e00\u81f4\uff0c\u5927\u7ea615\u6beb\u79d2\u3002\u4f46\u56e0\u4e3a\u662f\u57fa\u4e8e\u7ebf\u7a0b\u6c60\u7684\uff0c\u6240\u4ee5\u5728\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u8f83\u957f\u6216\u8005\u7ebf\u7a0b\u6c60\u8fc7\u8f7d\u65f6\uff0c\u4f1a\u51fa\u73b0\u5ef6\u8fdf\u3002\u5176\u7f3a\u70b9\u662f\u4f7f\u7528\u4e0d\u592a\u65b9\u4fbf\uff0c\u5b9a\u65f6\u5668\u521b\u5efa\u540e\u65e0\u6cd5\u4fee\u6539\u56de\u8c03\u65b9\u6cd5\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nvar stateTimer = new \nvar autoEvent = new AutoResetEvent(false);\nTimer(CheckStatus, autoEvent, 1000,250);\n\nprivate int invokeCount=0;\n\npublic void CheckStatus(Object stateInfo)\n{\n    AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;\n    Console.WriteLine(&quot;{0} Checking status {1,2}.&quot;,DateTime.Now.ToString(&quot;h:mm:ss.fff&quot;),(++invokeCount).ToString());\n\n    if(invokeCount == 10)\n    {\n        invokeCount = 0;\n        autoEvent.Set();\n    }\n}\n<\/pre>\n<\/p>\n<h3>System.Timers.Timer<\/h3>\n<p>System.Timers.Timer\u5728\u5185\u90e8\u4f7f\u7528System.Threading.Timer\uff0c\u5e76\u516c\u5f00\u4e86\u66f4\u591a\u7684\u5c5e\u6027\uff0c\u5982AutoReset, Enabled\u6216SynchronizingObject\uff0c\u8fd9\u4e9b\u5c5e\u6027\u5141\u8bb8\u914d\u7f6e\u56de\u8c03\u7684\u6267\u884c\u65b9\u5f0f\u3002\u6b64\u5916\uff0cTick\u4e8b\u4ef6\u5141\u8bb8\u6ce8\u518c\u591a\u4e2a\u5904\u7406\u7a0b\u5e8f\u3002\u56e0\u6b64\uff0c\u4e00\u4e2a\u5b9a\u65f6\u5668\u53ef\u4ee5\u89e6\u53d1\u591a\u4e2a\u5904\u7406\u7a0b\u5e8f\u3002\u8fd8\u53ef\u4ee5\u5728\u8ba1\u65f6\u5668\u542f\u52a8\u540e\u66f4\u6539\u5904\u7406\u7a0b\u5e8f\u3002\u4e0eSystem.Threading.Timer\u76f8\u4f3c\uff0c\u5176\u4f18\u70b9\u4e5f\u662f\u7cbe\u5ea6\u76f8\u5bf9\u8f83\u9ad8\uff0c\u4e0eWindows\u64cd\u4f5c\u7cfb\u7edf\u65f6\u949f\u7cbe\u5ea6\u4e00\u81f4\uff0c\u5927\u7ea615\u6beb\u79d2\u3002\u56e0\u4e3a\u9ed8\u8ba4\uff08\u6216\u8005SynchronizingObject=null\u65f6\uff09\u662f\u57fa\u4e8e\u7ebf\u7a0b\u6c60\u7684\uff0c\u6240\u4ee5\u5728\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u8f83\u957f\u6216\u8005\u7ebf\u7a0b\u6c60\u8fc7\u8f7d\u65f6\uff0c\u4f1a\u51fa\u73b0\u5ef6\u8fdf\u3002\u4f46\u4f7f\u7528\u8981\u66f4\u7b80\u4fbf\u4e00\u4e9b\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic partial class TimerFrom : Form\n{\n    private System.Timers.Timer timer;\n    private void TimerFrom_Load(object sender, EventArgs e)\n    {\n        \/\/ \u652f\u6301\u6ce8\u518c\u591a\u4e2a\u5904\u7406\u7a0b\u5e8f\n        timer.Elapsed += (sender, e) =&gt; { label1.Text = DateTime.Now.ToLongTimeString(); };\n        timer.Elapsed += (sender, e) =&gt; { Console.WriteLine(DateTime.Now.ToLongTimeString()); };\n        \/\/\u81ea\u5b9a\u4e49\u56de\u8c03\u6267\u884c\u7684\u65b9\u5f0f\uff08\u6307\u5b9a\u5bf9\u8c61\u6240\u5728\u7684\u7ebf\u7a0b\uff09\uff0cSynchronizingObject=null\u65f6\u5728\u7ebf\u7a0b\u6c60\u4e0a\u6267\u884c\n        timer.SynchronizingObject = this;\n        timer.AutoReset = true;\n        timer.Start();\n    }\n}\n<\/pre>\n<\/p>\n<p>\u672c\u4f8b\u4e2d\u5c06SynchronizingObject\u5c5e\u6027\u8bbe\u7f6e\u4e3aForm\u5bf9\u8c61\uff0c\u56e0\u6b64Elapsed\u7684\u5904\u7406\u7a0b\u5e8f\u5728UI\u7ebf\u7a0b\u4e0a\u6267\u884c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539label1.Text\uff0c\u5982\u679cSynchronizingObject\u5c5e\u6027\u4e3anull\uff0c\u5904\u7406\u7a0b\u5e8f\u5219\u662f\u5728\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u4e0a\u6267\u884c\uff0c\u4fee\u6539label1.Text\u65f6\u9700\u8981\u901a\u8fc7Invoke\u6216\u8005BeginInvoke\u5c01\u9001(marshal)\u5230UI\u7ebf\u7a0b\u4e0a\u8fd0\u884c\u3002<\/p>\n<h3>System.Threading.PeriodicTimer<\/h3>\n<p>System.Threading.PeriodicTimer\u662f .NET 6\u4e2d\u5f15\u5165\u7684\u5b9a\u65f6\u5668\u3002\u5b83\u80fd\u65b9\u4fbf\u5730\u4f7f\u7528\u5f02\u6b65\u65b9\u5f0f\uff0c\u5b83\u6ca1\u6709Tick\u4e8b\u4ef6\uff0c\u800c\u662f\u63d0\u4f9bWaitForNextTickAsync\u65b9\u6cd5\u5904\u7406\u5b9a\u65f6\u4efb\u52a1\u3002\u901a\u5e38\u662f\u4f7f\u7528While\u5faa\u73af\u7ed3\u5408CancellationToken\u4e00\u8d77\u4f7f\u7528\u3002\u548cCancellationToken\u4e00\u8d77\u7528\u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\uff0c\u5982\u679cCancellationToken\u88ab\u53d6\u6d88\u7684\u65f6\u5019\u4f1a\u629b\u51fa\u4e00\u4e2aOperationCanceledException\u9700\u8981\u8003\u8651\u81ea\u5df1\u5904\u7406\u5f02\u5e38\u3002\u76f8\u6bd4\u4e4b\u524d\u7684\u5b9a\u65f6\u5668\u6765\u8bf4\uff0c\u6709\u4e0b\u9762\u51e0\u4e2a\u7279\u70b9\uff1a<\/p>\n<p>1.\u6ca1\u6709callback \u6765\u7ed1\u5b9a\u4e8b\u4ef6;<br \/>\n2.\u4e0d\u4f1a\u53d1\u751f\u91cd\u5165\uff0c\u53ea\u5141\u8bb8\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\uff0c\u4e0d\u5141\u8bb8\u540c\u4e00\u4e2aPeriodicTimer\u5728\u4e0d\u540c\u7684\u5730\u65b9\u540c\u65f6WaitForNextTickAsync\uff0c\u4e0d\u9700\u8981\u81ea\u5df1\u505a\u6392\u4ed6\u9501\u6765\u5b9e\u73b0\u4e0d\u80fd\u91cd\u5165\uff1b<br \/>\n3.\u5f02\u6b65\u5316\u3002\u4e4b\u524d\u7684 timer \u7684 callback \u90fd\u662f\u540c\u6b65\u7684\uff0c\u4f7f\u7528\u65b0 timer \u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u65b9\u6cd5\uff0c\u907f\u514d\u4e86\u7f16\u5199 Sync over Async \u4ee3\u7801\uff1b<br \/>\n4.Dispose \u4e4b\u540e\uff0c\u5b9e\u4f8b\u5c31\u65e0\u6cd5\u4f7f\u7528\uff0c\u5e76\u4e14 WaitForNextTickAsync \u59cb\u7ec8\u8fd4\u56de false\u3002<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nvar cts = new CancellationTokenSource(TimeSpan.FromSeconds(15));\nusing (var timer = new PeriodicTimer(TimeSpan.FromSeconds(1)))\n{\n    try\n    {\n        while (await timer.WaitForNextTickAsync(cts.Token))\n        {\n            await Task.Delay(3000);\n            Console.WriteLine($&quot;ThreadId is {Thread.CurrentThread.ManagedThreadId} --- Time is {DateTime.Now:HH:mm:ss}&quot;);\n        }\n    }\n    catch (OperationCanceledException)\n    {\n        Console.WriteLine(&quot;Operation cancelled&quot;);\n    }\n}\n<\/pre>\n<\/p>\n<p>\u5c0f\u7ed3<br \/>\n\u6211\u4eec\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u5751\u5f80\u5f80\u4e0d\u662f\u6280\u672f\u672c\u8eab\u7684\u5751\uff0c\u800c\u662f\u6211\u4eec\u6ee5\u7528\u6ca1\u6709\u638c\u63e1\u7684\u6280\u672f\u5bfc\u81f4\u7684\uff0c\u5728\u6709\u591a\u79cd\u6280\u672f\u65b9\u6848\u53ef\u9009\u7684\u65f6\u5019\uff0c\u901a\u5e38\u53ea\u5173\u6ce8\u6280\u672f\u7684\u4f18\u70b9\uff0c\u5ffd\u7565\u4e86\u6280\u672f\u9002\u7528\u573a\u666f\u53ca\u5176\u5c40\u9650\u6027\u3002.NET\u4e2d\u51e0\u79cd\u5b9a\u65f6\u5668\u5404\u81ea\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4e0d\u8db3\uff0c\u4f46\u90fd\u4e0d\u652f\u6301\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u3002\u4e86\u89e3\u8fd9\u4e9b\u6709\u52a9\u4e8e\u6211\u4eec\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u9009\u62e9\u5408\u9002\u5b9a\u65f6\u5668\uff0c\u907f\u514d\u9047\u5230\u95ee\u9898\u540e\u88ab\u52a8\u5730\u66ff\u6362\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u539f\u6587\uff1a<a href=\"https:\/\/www.cnblogs.com\/czwy\/p\/17862702.html\"><a href=\"https:\/\/www.cnblogs.com\/czwy\/p\/17862702.html\">https:\/\/www.cnblogs.com\/czwy\/p\/17862702.html<\/a><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>.NET\u4e2d\u81f3\u5c11\u67096\u79cd\u5b9a\u65f6\u5668\uff0c\u6bcf\u4e00\u79cd\u5b9a\u65f6\u5668\u90fd\u6709\u5b83\u7684\u7528\u9014\u548c\u7279\u70b9\u3002\u6839\u636e\u5b9a\u65f6\u5668\u7684\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5206\u4e3aUI\u76f8\u5173\u7684\u5b9a\u65f6\u5668\u548cUI\u65e0\u5173\u7684\u5b9a\u65f6\u5668\u3002\u672c\u6587\u5c06\u7b80\u5355\u4ecb\u7ecd\u8fd96\u79cd\u5b9a\u65f6\u5668\u7684\u57fa\u672c\u7528\u6cd5\u548c\u7279\u70b9\u3002 UI\u5b9a\u65f6\u5668# .NET\u4e2d\u7684UI\u5b9a\u65f6\u5668\u4e3b\u8981\u662fWinForm\u3001WPF\u4ee5\u53caWebForm\u4e2d\u7684\u5b9a\u65f6\u5668\u3002\u5206\u522b\u4e3a\uff1a System.Windows.Forms.Timer System.Windows.Threading.DispatcherTimer System.Web.UI.Timer \u901a\u5e38\u60c5\u51b5\u4e0b\uff0cWinForm\u3001WPF\u4e2d\u7684\u5b9a\u65f6\u5668\u662f\u5728UI\u7ebf\u7a0b\u4e0a\u6267\u884c\u56de\u8c03\u51fd\u6570\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95eeUI\u5143\u7d20\u3002\u7531\u4e8eWinForm\u3001WPF\u652f\u6301\u5355\u7ebf\u7a0b\u5355\u5143\u6a21\u578b\uff08Single-Thread Apartment\uff0cSTA\uff09\uff0c\u5b9a\u65f6\u5668\u95f4\u9694\u4e8b\u4ef6\u662f\u5728UI\u7ebf\u7a0b\u4e0a\u89e6\u53d1\uff0c\u56e0\u6b64\uff0c\u4e0d\u7528\u62c5\u5fc3\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002 System.Web.UI.Timer\u662f\u901a\u8fc7Javascript\u5b9a\u65f6\u5668\u548c\u670d\u52a1\u7aef\u5f02\u6b65\u56de\u8c03\u5b9e\u73b0\uff0c\u4e5f\u662f\u5355\u7ebf\u7a0b\u7684\u3002 \u8bf7\u6ce8\u610f\uff0c\u8fd9\u91cc\u8bf4\u7684\u662f\u901a\u5e38\u60c5\u51b5\uff0c\u540e\u8fb9\u4ecb\u7ecdSystem.Windows.Threading.DispatcherTimer\u65f6\u4f1a\u63d0\u5230\u5728\u975eUI\u7ebf\u7a0b\u521b\u5efaDispatcherTimer\u65f6\u4e5f\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95eeUI\u5143\u7d20\u3002 System.Windows.Forms.Timer System.Windows.Forms.Timer\u9488\u5bf9WinForm\u5e94\u7528\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u662f\u53ea\u80fd\u5728WinForm\u4e0a\u4f7f\u7528\u7684\u5b9a\u65f6\u5668\u3002\u8fd9\u4e2a\u5b9a\u65f6\u5668\u662f\u9488\u5bf9\u5355\u7ebf\u7a0b\u73af\u5883\u8bbe\u8ba1\u7684\uff0c\u662f\u5728UI\u7ebf\u7a0b\u4e0a\u5904\u7406\u5b9a\u65f6\u4efb\u52a1\u3002 \u5b83\u8981\u6c42\u7528\u6237\u4ee3\u7801\u6709\u53ef\u7528\u7684UI\u6d88\u606f\u6cf5\uff0c\u5b9a\u65f6\u4efb\u52a1\u987b\u5728UI\u7ebf\u7a0b\u4e0a\u8fd0\u884c\uff0c\u6216\u8005\u8de8\u7ebf\u7a0b\u901a\u8fc7Invoke\u6216\u8005BeginInvoke\u5c01\u9001(marshal)\u5230UI\u7ebf\u7a0b\u4e0a\u8fd0\u884c\u3002\u5176\u4f18\u70b9\u662f\u4f7f\u7528\u7b80\u5355\uff0c\u53ea\u9700\u901a\u8fc7\u7ed9Interval\u5c5e\u6027\u8d4b\u503c\u6765\u8bbe\u7f6e\u65f6\u95f4\u95f4\u9694\uff0c\u5e76\u6ce8\u518cTick\u4e8b\u4ef6\u5904\u7406\u5b9a\u65f6\u4efb\u52a1\u3002\u5176\u7f3a\u70b9\u662f\u7cbe\u5ea6\u4e0d\u9ad8\uff0c\u7cbe\u5ea6\u4e3a55\u6beb\u79d2\uff0c\u4e5f\u5c31\u662fInterval\u8d4b\u503c\u5c0f\u4e8e55\u65f6\uff0c\u4e5f\u662f55\u6beb\u79d2\u89e6\u53d1\u4e00\u6b21\u5b9a\u65f6\u4efb\u52a1\u3002 public partial class TimerFrom : Form { private System.Windows.Forms.Timer digitalClock; private void TimerFrom_Load(object sender, EventArgs e) { digitalClock = new System.Windows.Forms.Timer();\/\/\u521b\u5efa\u5b9a\u65f6\u5668 digitalClock.Tick += new EventHandler(HandleTime);\/\/\u6ce8\u518c\u5b9a\u65f6\u4efb\u52a1\u4e8b\u4ef6 digitalClock.Interval = 1000;\/\/\u8bbe\u7f6e\u65f6\u95f4\u95f4\u9694 digitalClock.Enabled = true; digitalClock.Start(); \/\/\u5f00\u542f\u5b9a\u65f6\u5668 } public void HandleTime(Object myObject, EventArgs myEventArgs) { labelClock.Text [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-410","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=410"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/410\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}