C#掌握台下測試多線程的辦法。本站提示廣大學習愛好者:(C#掌握台下測試多線程的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#掌握台下測試多線程的辦法正文
本文實例講述了C#掌握台下多線程完成辦法。分享給年夜家供年夜家參考。詳細以下:
class Program { static void Main(string[] args) { ThreadStart num = new ThreadStart(PrintNum); Thread ConstrolNum = new Thread(num); ThreadStart str = new ThreadStart(PrintStr); Thread ConstrolStr = new Thread(str); Stopwatch watch = new Stopwatch(); watch.Start(); ConstrolNum.Start(); ConstrolStr.Start(); while (true) { if (ConstrolNum.ThreadState == System.Threading.ThreadState.Stopped && ConstrolStr.ThreadState == System.Threading.ThreadState.Stopped) { watch.Stop(); Console.WriteLine(watch.Elapsed.TotalMilliseconds); break; } } Console.ReadKey(); } private static void PrintNum() { for (int i = 1; i < 1000; i++) { Console.WriteLine(i); } } private static void PrintStr() { for (int i = 1; i < 1000; i++) { Console.WriteLine("以後數為:{0}", i); } } }
願望本文所述對年夜家的C#法式設計有所贊助。