国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

多線程編程(2.Timer)

2019-11-06 06:02:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Threading.Timer

internal sealed class UnSafelTimer { PRivate static Timer timer; private static object unsafel_lock = new object(); private static int count = 0; private static Stopwatch watch = new Stopwatch(); private static void ExcuteMethod(object state) { //不是后臺(tái)線程 Thread.CurrentThread.IsBackground = true; int temp; lock (unsafel_lock) { count++; temp = count; } if (temp == 10) { timer.Dispose(); watch.Stop(); } if (temp < 10) Console.WriteLine("NOW:{0}", temp); else Console.WriteLine("NOW:{0},-------Timer已經(jīng)耗時(shí){1}毫秒了", temp, watch.ElapsedMilliseconds); //模擬花10秒時(shí)間 Thread.Sleep(1000); } public static void Start() { watch.Start(); //2秒鐘后每次間隔10毫秒就啟動(dòng)定時(shí)器 timer = new Timer(ExcuteMethod, null, 1000, 10); } }

Timer.Timer

internal sealed class SafelTimer { private static System.Timers.Timer timer; private static object unsafel_lock = new object(); private static int count = 0; private static Stopwatch watch = new Stopwatch(); public static void Start() { timer = new System.Timers.Timer(); timer.Interval = 10;//每隔10毫秒執(zhí)行 timer.Elapsed += ExcuteMethod; timer.Start(); } private static void ExcuteMethod(object sender, System.Timers.ElapsedEventArgs e) { Thread.CurrentThread.IsBackground = true; int temp; lock (unsafel_lock) { count++; temp = count; } if (temp == 10) { timer.Dispose(); watch.Stop(); } if (temp < 10) Console.WriteLine("NOW:{0}", temp); else Console.WriteLine("NOW:{0},-------Timer已經(jīng)耗時(shí){1}毫秒了", temp, watch.ElapsedMilliseconds); //模擬花10秒時(shí)間 Thread.Sleep(1000); } }
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 玛纳斯县| 昌都县| 荣成市| 广平县| 彭山县| 司法| 广安市| 龙口市| 开封市| 轮台县| 安泽县| 安西县| 简阳市| 汉中市| 郸城县| 高青县| 亚东县| 安远县| 娱乐| 兰溪市| 平舆县| 吕梁市| 景洪市| 南安市| 周口市| 溆浦县| 息烽县| 常德市| 佛坪县| 江口县| 增城市| 福泉市| 固镇县| 温宿县| 广汉市| 茂名市| 中牟县| 会东县| 昌图县| 田林县| 宿迁市|