国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 客服| 类乌齐县| 驻马店市| 惠来县| 桂林市| 沙雅县| 双柏县| 昌邑市| 乌鲁木齐县| 金湖县| 静宁县| 喀喇沁旗| 手机| 慈利县| 安达市| 海伦市| 萨迦县| 西青区| 屏南县| 乌鲁木齐县| 丰县| 兰溪市| 平塘县| 东台市| 曲沃县| 土默特左旗| 靖安县| 兖州市| 建平县| 高密市| 大安市| 佛冈县| 新宁县| 汉川市| 云霄县| 称多县| 泊头市| 嘉义县| 仙游县| 通化县| 宣汉县|