用回調(diào)方法檢索數(shù)據(jù)
          下面的示例演示了一個從線程中檢索數(shù)據(jù)的回調(diào)方法。包含數(shù)據(jù)和線程方法的類的構(gòu)造函數(shù)也接受代表回調(diào)方法的委托;
          在線程方法結(jié)束前,它調(diào)用該回調(diào)委托。
          */
          using System;
          using System.Threading;

          //被實例化的類,用于傳遞參數(shù)進(jìn)行操作
          public class ThreadWithState 
          {
              
          private string boilerplate;
              
          private int value;
              
          //定義一個回調(diào)函數(shù)
              private ExampleCallback callback;

              
          //接收線程傳遞的參數(shù)
              public ThreadWithState(string text, int number, ExampleCallback callbackDelegate) 
              
          {
                  boilerplate 
          = text;
                  value 
          = number;
                  callback 
          = callbackDelegate;
              }


              
          public void ThreadProc() 
              
          {
                  Console.WriteLine(boilerplate, value);
                  
          //判斷是否執(zhí)行回調(diào)函數(shù)、并把參數(shù)傳遞給回調(diào)函數(shù)
                  if (callback != null)
                      callback(
          1);
              }

          }


          //聲明一個回調(diào)函數(shù):注意傳遞的參數(shù)要與Example類中的函數(shù)參數(shù)類型一致
          public delegate void ExampleCallback(int lineCount);

          //事例主類
          public class Example 
          {
              
          public static void Main() 
              
          {
                  
          //實例化ThreadWithState,并傳遞參數(shù),注意:new ExampleCallback(ResultCallback)的命名空間引用,及其傳遞的參數(shù)(ResultCallback函數(shù))
                  ThreadWithState tws = new ThreadWithState("This number {0}."42new ExampleCallback(ResultCallback));

                  Thread t 
          = new Thread(new ThreadStart(tws.ThreadProc));
                  t.Start();
                  Console.WriteLine(
          "線程開始執(zhí)行");
                  
          //此為判斷線程是否執(zhí)行結(jié)束,但不建議阻塞線程,這樣線程就會等待完成后再進(jìn)行其他操作
                  
          //可利用:t.IsAlive是否處于執(zhí)行狀態(tài)來判斷線程
                  t.Join();
                  Console.WriteLine(
          "線程執(zhí)行結(jié)束");  
              }


              
          //回調(diào)處理函數(shù),應(yīng)與ExampleCallback的參數(shù)類型應(yīng)一致
              public static void ResultCallback(int lineCount) 
              
          {
                  Console.WriteLine(
          "printed {0} lines.", lineCount);  
              }

          }



          /*向線程傳遞數(shù)據(jù)
          ThreadStart 委托既沒有參數(shù)也沒有返回值。這意味著不可以使用需要參數(shù)的方法啟動線程,或從方法中獲得返回值。 

          為向線程傳遞數(shù)據(jù),需要創(chuàng)建一個用來保持?jǐn)?shù)據(jù)和線程方法的對象,如下面的兩個代碼示例所示。 
          為檢索線程方法的結(jié)果,您可以使用回調(diào)方法,如第二個代碼示例中所示。 
          */

          using System;
          using System.Threading;

          //被實例化的類,用于傳遞參數(shù)進(jìn)行操作
          public class ThreadWithState 
          {
              
          private string boilerplate;
              
          private int value;

              
          public ThreadWithState(string text, int number) 
              
          {
                  boilerplate 
          = text;
                  value 
          = number;
              }

                     
              
          public void ThreadProc() 
              
          {
                  Console.WriteLine(boilerplate, value); 
              }

          }


          //創(chuàng)建線程調(diào)用ThreadWithState
          public class Example 
          {
              
          public static void Main() 
              
          {
                  
          //實例化類ThreadWithState,利用構(gòu)造函數(shù)向線程傳遞參數(shù)
                  ThreadWithState tws = new ThreadWithState("This number {0}."42);
                  
          //創(chuàng)建線程并執(zhí)行ThreadWithState類中的ThreadProc函數(shù)
                  Thread t = new Thread(new ThreadStart(tws.ThreadProc));
                  t.Start();
                  Console.WriteLine(
          "線程開始執(zhí)行");
                  
          //此為判斷線程是否執(zhí)行結(jié)束,但不建議阻塞線程,這樣線程就會等待完成后再進(jìn)行其他操作
                  
          //可利用:t.IsAlive是否處于執(zhí)行狀態(tài)來判斷線程
                  t.Join();
                  Console.WriteLine(
          "線程執(zhí)行結(jié)束");  
              }

          }

          文章來源:http://www.cnblogs.com/wangdetian168/archive/2008/12/21/1359255.html
          posted on 2010-10-11 09:28 sanmao 閱讀(281) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 天祝| 石台县| 阿拉善左旗| 梓潼县| 玛多县| 逊克县| 巫山县| 鄱阳县| 仪陇县| 丰镇市| 繁昌县| 印江| 盖州市| 温州市| 江川县| 加查县| 呼玛县| 房产| 高邮市| 浦北县| 鲁山县| 和政县| 汉中市| 台中县| 梧州市| 谷城县| 永定县| 明光市| 读书| 荆州市| 吕梁市| 通江县| 微山县| 苏州市| 延安市| 搜索| 舒兰市| 封丘县| 清河县| 武平县| 永泰县|