六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

在C#中編寫多線程應用程序,容易!

[摘要]來自:www.kunwsoft.com 以前在使用VB來實現多線程的時候,發現有一定的難度。雖然也有這樣那樣的方法,但都不盡人意,但在C#中,要編寫多線程應用程序卻相當的簡單。這篇文章將作簡要的介紹,以起到拋磚引玉的作用! .NET將關于多線程的功能定義在System.Threadin...

來自:www.kunwsoft.com

    以前在使用VB來實現多線程的時候,發現有一定的難度。雖然也有這樣那樣的方法,但都不盡人意,但在C#中,要編寫多線程應用程序卻相當的簡單。這篇文章將作簡要的介紹,以起到拋磚引玉的作用!
    .NET將關于多線程的功能定義在System.Threading名字空間中。因此,要使用多線程,必須先聲明引用此名字空間(using System.Threading;)。
       即使你沒有編寫多線程應用程序的經驗,也可能聽說過“啟動線程”“殺死線程”這些詞,其實除了這兩個外,涉及多線程方面的還有諸如“暫停線程”“優先級”“掛起線程”“恢復線程”等等。下面將一個一個的解釋。
    a.啟動線程
    顧名思義,“啟動線程”就是新建并啟動一個線程的意思,如下代碼可實現:
    Thread thread1 = new Thread(new ThreadStart( Count));
    其中的 Count 是將要被新線程執行的函數。
    b.殺死線程
    “殺死線程”就是將一線程斬草除根,為了不白費力氣,在殺死一個線程前最好先判斷它是否還活著(通過 IsAlive 屬性),然后就可以調用 Abort 方法來殺死此線程。
    c.暫停線程
    它的意思就是讓一個正在運行的線程休眠一段時間。如 thread.Sleep(1000); 就是讓線程休眠1秒鐘。
    d.優先級
    這個用不著解釋了。Thread類中有一個ThreadPriority屬性,它用來設置優先級,但不能保證操作系統會接受該優先級。一個線程的優先級可分為5種:Normal, AboveNormal, BelowNormal, Highest, Lowest。具體實現例子如下:
    thread.Priority = ThreadPriority.Highest;
    e.掛起線程
    Thread類的Suspend方法用來掛起線程,知道調用Resume,此線程才可以繼續執行。如果線程已經掛起,那就不會起作用。
    if (thread.ThreadState = ThreadState.Running)
    {
         thread.Suspend();
    }
    f.恢復線程
    用來恢復已經掛起的線程,以讓它繼續執行,如果線程沒掛起,也不會起作用。
    if (thread.ThreadState = ThreadState.Suspended)
    {
         thread.Resume();
    }
    下面將列出一個例子,以說明簡單的線程處理功能。此例子來自于幫助文檔。
    using System;
    using System.Threading;

    // Simple threading scenario:  Start a static method running
    // on a second thread.
    public class ThreadExample {
        // The ThreadProc method is called when the thread starts.
        // It loops ten times, writing to the console and yielding
        // the rest of its time slice each time, and then ends.
        public static void ThreadProc() {
            for (int i = 0; i < 10; i++) {
                Console.WriteLine("ThreadProc: {0}", i);
                // Yield the rest of the time slice.
                Thread.Sleep(0);
            }
        }
   
        public static void Main() {
            Console.WriteLine("Main thread: Start a second thread.");
            // The constructor for the Thread class requires a ThreadStart
            // delegate that represents the method to be executed on the
            // thread.  C# simplifies the creation of this delegate.
            Thread t = new Thread(new ThreadStart(ThreadProc));
            // Start ThreadProc.  On a uniprocessor, the thread does not get
            // any processor time until the main thread yields.  Uncomment
            // the Thread.Sleep that follows t.Start() to see the difference.
            t.Start();
            //Thread.Sleep(0);
   
            for (int i = 0; i < 4; i++) {
                Console.WriteLine("Main thread: Do some work.");
                Thread.Sleep(0);
            }
   
            Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
            t.Join();
            Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
            Console.ReadLine();
        }
    }
   
       此代碼產生的輸出類似如下內容:

    Main thread: Start a second thread.
    Main thread: Do some work.
    ThreadProc: 0
    Main thread: Do some work.
    ThreadProc: 1
    Main thread: Do some work.
    ThreadProc: 2
    Main thread: Do some work.
    ThreadProc: 3
    Main thread: Call Join(), to wait until ThreadProc ends.
    ThreadProc: 4
    ThreadProc: 5
    ThreadProc: 6
    ThreadProc: 7
    ThreadProc: 8
    ThreadProc: 9
    Main thread: ThreadProc.Join has returned.  Press Enter to end program.




主站蜘蛛池模板: 四虎永久免费网站免费观看 | 伊人久久影院 | 天天影视色香欲综合网天天录日日录 | 污污网站国产精品白丝袜 | 婷婷射丁香 | 亚洲色域网| 欧美综合图区亚欧综合图区 | 天堂亚洲国产日韩在线看 | 一级黄色在线视频 | 色成人亚洲 | 色综合久久精品中文字幕 | 一区二区三区在线观看视频 | 日韩欧美亚洲国产精品字幕久久久 | 日本xxxwww在线观看免费 | 日本中出 | 午夜免费片| 手机看片福利在线 | 午夜三级福利 | 青青青爽线在线视频观看 | 中文字幕专区在线亚洲 | 日韩一级视频免费观看 | 日日摸人人看97人人澡 | 欧美性视屏 | 奇米狠狠色| 亚洲欧洲免费视频 | 色偷偷亚洲男人天堂 | 最近更新在线中文字幕一页 | 日本一本在线播放 | 天天爽天天狼久久久综合 | 日韩中文网 | 亚洲 丝袜 制服 欧美 另类 | 青娱极品盛宴国产精品分类 | 日韩精品一区二三区中文 | 在线视频一区二区日韩国产 | 日日射影院 | 日本久久免费大片 | 色天使在线播放 | 午夜在线观看免费高清在线播放 | 一级做受视频免费是看美女 | 日韩午夜在线视频不卡片 | 日韩欧美一区二区三区不卡在线 |