熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

理解.NET中Thread.Sleep()方法(C#)

2022-06-13   來源: .NET編程 

  using System

  using SystemThreading

  namespace ThreadTest

  {

  public class A { public static void B()

  { while (true)

  { ConsoleWriteLine(AB is running in its own thread}

  public class C { public static void Main()

  { Thread t = new Thread(new ThreadStart(AB))tStart()ThreadSleep(tAbort()tJoin()ConsoleWriteLine(AB has finishedConsoleReadLine()}

  }

  運行後顯示若干行AB is running in its own thread和一行AB has finished

  若取消程序中的ThreadSleep(則運行後只顯示AB has finished

  理解如無ThreadSleep(則線程oThread一開始後即被中止根據沒有執行其中的代碼但增加此句後使主線程sleep毫秒這段時間使thread獲得了資源得到了執行


From:http://tw.wingwit.com/Article/program/net/201311/13247.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.