C#線程系列講座(1):BeginInvoke和EndInvoke方法

開發語言: C#3.0

IDE Visual Studio 2008

本系列教程主要包括如下內容:

1.  BeginInvoke
EndInvoke方法

2.  Thread

3. 線程池

4. 線程同步基礎

5. 死鎖

6. 線程同步的 7種方法

7. 如何在線程中訪問 GUI組件

一、線程概述

在操作系統中一個進程至少要包含一個線程,然後,在某些時候需要在同一個進程中同時執行多項任務,或是爲了提供程序的性能,將要執行的任務分解成多個子任務執行。這就需要在同一個進程中開啓多個線程。我們使用 C# 編寫一個應用程序(控制檯或桌面程序都可以),然後運行這個程序,並打開 windows 任務管理器,這時我們就會看到這個應用程序中所含有的線程數,如下圖所示。



<!-- /* Font Definitions */ &#64;font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

如果任務管理器沒有“線程數”列,可以【查看】 >【選擇列】來顯示“線程計數”列。從上圖可以看出,幾乎所有的進程都擁有兩個以上的線程。從而可以看出,線程是提供應用程序性能的重要手段之一,尤其在多核 CPU的機器上尤爲明顯。

二、用委託 (Delegate) BeginInvoke EndInvoke 方法操作線程

 

C#中使用線程的方法很多,使用委託的 BeginInvoke EndInvoke方法就是其中之一。 BeginInvoke方法可以使用線程異步地執行委託所指向的方法。然後通過 EndInvoke方法獲得方法的返回值( EndInvoke方法的返回值就是被調用方法的返回值),或是確定方法已經被成功調用。我們可以通過四種方法從 EndInvoke方法來獲得返回值。

三、直接使用 EndInvoke 方法來獲得返回值

    當使用 BeginInvoke 異步調用方法時,如果方法未執行完, EndInvoke 方法就會一直阻塞,直到被調用的方法執行完畢。如下面的代碼所示:

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Threading;

namespace  MyThread
{
    
class  Program
    {
        
private   static   int  newTask( int  ms)
        {
            Console.WriteLine(
" 任務開始 " );
            Thread.Sleep(ms);
            Random random 
=   new  Random();
            
int  n  =  random.Next( 10000 );
            Console.WriteLine(
" 任務完成 " );
            
return  n;
        }

        
private   delegate   int  NewTaskDelegate( int  ms);
             
        
        
static   void  Main( string [] args)
        {
            NewTaskDelegate task 
=  newTask;
            IAsyncResult asyncResult 
=  task.BeginInvoke( 2000 null null );

            //  EndInvoke方法將被阻塞2秒
             int  result  =  task.EndInvoke(asyncResult);           
            Console.WriteLine(result);
        }
    }
}

<!-- /* Font Definitions */ &#64;font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    在運行上面的程序後,由於 newTask方法通過 Sleep延遲了 2秒,因此,程序直到 2秒後才輸出最終結果(一個隨機整數)。如果不調用 EndInvoke方法,程序會立即退出,這是由於使用 BeginInvoke創建的線程都是後臺線程,這種線程一但所有的前臺線程都退出後(其中主線程就是一個前臺線程),不管後臺線程是否執行完畢,都會結束線程,並退出程序。關於前臺和後臺線程的詳細內容,將在後面的部分講解。

    讀者可以使用上面的程序做以下實驗。首先在 Main方法的開始部分加入如下代碼:

Thread.Sleep(10000);

    以使 Main方法延遲 10秒鐘再執行下面的代碼,然後按 Ctrl+F5運行程序,並打開企業管理器,觀察當前程序的線程數,假設線程數是 4,在 10秒後,線程數會增至 5,這是因爲調用 BeginInvoke方法時會建立一個線程來異步執行 newTask方法,因此,線程會增加一個。

四、使用 IAsyncResult asyncResult 屬性來判斷異步調用是否完成

   
雖然上面的方法可以很好地實現異步調用,但是當調用 EndInvoke 方法獲得調用結果時,整個程序就象死了一樣,這樣做用戶的感覺並不會太好,因此,我們可以使用 asyncResult 來判斷異步調用是否完成,並顯示一些提示信息。這樣做可以增加用戶體驗。代碼如下:

static   void  Main( string [] args)
{
    NewTaskDelegate task 
=  newTask;
    IAsyncResult asyncResult 
=  task.BeginInvoke( 2000 null null );
 
    
while  ( ! asyncResult.IsCompleted)
    {
        Console.Write(
" * " );
        Thread.Sleep(
100 );
    }
    
//  由於異步調用已經完成,因此, EndInvoke會立刻返回結果
    int  result  =  task.EndInvoke(asyncResult);           
    Console.WriteLine(result);
}

<!-- /* Font Definitions */ &#64;font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->     上面代碼的執行結果如下圖所示。



<!-- /* Font Definitions */ &#64;font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    由於是異步,所以“ *”可能會在“任務開始”前輸出,如上圖所示。


五、使用 WaitOne 方法等待異步方法執行完成

   
使用 WaitOne 方法是另外一種判斷異步調用是否完成的方法。代碼如下:

static   void  Main( string [] args)
{
    NewTaskDelegate task 
=  newTask;
    IAsyncResult asyncResult 
=  task.BeginInvoke( 2000 null null );

    
while  ( ! asyncResult.AsyncWaitHandle.WaitOne( 100 false ))
    {
         Console.Write(
" * " );              
    }

    
int  result  =  task.EndInvoke(asyncResult);
    Console.WriteLine(result);
}

<!-- /* Font Definitions */ &#64;font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    WaitOne 的第一個參數表示要等待的毫秒數,在指定時間之內, WaitOne方法將一直等待,直到異步調用完成,併發出通知, WaitOne方法才返回 true。當等待指定時間之後,異步調用仍未完成, WaitOne方法返回 false,如果指定時間爲 0,表示不等待,如果爲 -1,表示永遠等待,直到異步調用完成。

六、使用回調方式返回結果

    上面介紹的幾種方法實際上只相當於一種方法。這些方法雖然可以成功返回結果,也可以給用戶一些提示,但在這個過程中,整個程序就象死了一樣(如果讀者在 GUI 程序中使用這些方法就會非常明顯),要想在調用的過程中,程序仍然可以正常做其它的工作,就必須使用異步調用的方式。下面我們使用 GUI 程序來編寫一個例子,代碼如下:

private   delegate   int  MyMethod();
private   int  method()
{
    Thread.Sleep(
10000 );
    
return   100 ;
}
private   void  MethodCompleted(IAsyncResult asyncResult)
{
    
if  (asyncResult  ==   null return ;
    textBox1.Text 
=  (asyncResult.AsyncState  as  
    MyMethod).EndInvoke(asyncResult).ToString();
}

private   void  button1_Click( object  sender, EventArgs e)
{

    MyMethod my 
=  method;
    IAsyncResult asyncResult 
=  my.BeginInvoke(MethodCompleted, my);
}

<!-- /* Font Definitions */ &#64;font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    要注意的是,這裏使用了 BeginInvoke方法的最後兩個參數(如果被調用的方法含有參數的話,這些參數將作爲 BeginInvoke的前面一部分參數,如果沒有參數, BeginInvoke就只有兩個參數了)。第一個參數是回調方法委託類型,這個委託只有一個參數,就是 IAsyncResult, MethodCompleted方法所示。當 method方法執行完後,系統會自動調用 MethodCompleted方法。 BeginInvoke的第二個參數需要向 MethodCompleted方法中傳遞一些值,一般可以傳遞被調用方法的委託,如上面代碼中的 my。這個值可以使用 IAsyncResult.AsyncState屬性獲得。

    由於上面的代碼通過異步的方式訪問的 form上的一個 textbox,因此,需要按 ctrl+f5運行程序(不能直接按 F5運行程序,否則無法在其他線程中訪問這個 textbox,關於如果在其他線程中訪問 GUI組件,並在後面的部分詳細介紹)。並在 form上放一些其他的可視控件,然在點擊 button1後,其它的控件仍然可以使用,就象什麼事都沒有發生過一樣,在 10秒後,在 textbox1中將輸出 100

七、其他組件的 BeginXXX EndXXX 方法

    在其他的 .net組件中也有類似 BeginInvoke EndInvoke的方法,如 System.Net.HttpWebRequest類的 BeginGetResponse EndGetResponse方法,下面是使用這兩個方法的一個例子:


private   void  requestCompleted(IAsyncResult asyncResult)
{
    
if  (asyncResult  ==   null return ;
    System.Net.HttpWebRequest hwr 
=  asyncResult.AsyncState  as  System.Net.HttpWebRequest;
    System.Net.HttpWebResponse response 
=  
(System.Net.HttpWebResponse)hwr.EndGetResponse(asyncResult);
    System.IO.StreamReader sr 
=   new  
System.IO.StreamReader(response.GetResponseStream());
    textBox1.Text 
=  sr.ReadToEnd();
}
private   delegate  System.Net.HttpWebResponse RequestDelegate(System.Net.HttpWebRequest request);

private   void  button1_Click( object  sender, EventArgs e)
{
    System.Net.HttpWebRequest request 
=  
    (System.Net.HttpWebRequest)System.Net.WebRequest.Create(
" http://www.cnblogs.com " );
    IAsyncResult asyncResult 
= request.BeginGetResponse(requestCompleted, request);     
}
發佈了33 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章