C#之异步编程
多线程的分类
异步编程async/await
多线程异常处理
Timer线程
线程同步
本文档使用 MrDoc 发布
-
+
首页
Timer线程
# System.Threading.Timer ```C# class Person { public int Money = 0; } internal class Program { static void Main(string[] args) { Console.WriteLine("主线程开始"); Person p = new Person(); p.Money = 5; System.Threading.Timer timer = new Timer(state => { if (state is Person p1) { Console.WriteLine($"Money={p1.Money}"); p1.Money--; if (p1.Money == 0) { Environment.Exit(0); } } }, p, 500, 1000); Console.WriteLine("主线程结束"); Console.ReadKey(); } } ``` # System.Timers.Timer ```C# internal class Program { static System.Timers.Timer timer; static Person person = new Person(); static void Main(string[] args) { Console.WriteLine("主线程开始"); timer = new System.Timers.Timer(1000); timer.AutoReset = true; timer.Elapsed += Timer_Elapsed; timer.Start(); Task.Delay(5500).ContinueWith(t => { timer.Stop(); timer.Dispose(); }); Console.WriteLine("主线程结束"); Console.ReadKey(); } private static void Timer_Elapsed(object sender, ElapsedEventArgs e) { person.Money++; Console.WriteLine($"Money={person.Money}"); } } class Person { public int Money = 0; } ``` # System.WindowsForms.Timer # System.Windows.Threading.DispatcherTimer # System.ComponentModel.BackgroundWorker
张泽楠
2025年3月31日 18:57
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码