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

c#異步調用的幾種方式

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

  首先我們分析一下異步處理的環境

  需要在當前線程中獲取返回值

  不需要在當前線程中獲取返回值但是仍然需要對返回值做處理

  對於第中情況還可以繼續細分

  在當前線程中啟動線程T然後繼續執行當前線程中的其它任務最後在當前線程中獲取T的返回值

  在當前線程中啟動線程T然後繼續執行當前線程中的其它任務R等待T執行完成當T執行完成後繼續執行當前線程中的其它任務R最後獲取T的返回值

  在當前線程中啟動線程T只要T在執行就執行任務R最後獲取T的返回值

  下面我將一一給出例子

   在當前線程中啟動線程T然後繼續執行當前線程中的其它任務最後在當前線程中獲取T的返回值

   using System;

   using SystemCollectionsGeneric;

   using SystemLinq;

   using SystemWindowsForms;

   using SystemThreading;

   using SystemRuntimeRemotingMessaging;

   namespace FirstWF

   {

       static class Program

       {

           /// <summary>

           /// The main entry point for the application

           /// </summary>

           [STAThread]

           static void Main()

           {

               AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

               ConsoleWriteLine(Input number please);

               IAsyncResult result = callerBeginInvoke(ConvertToInt(ConsoleReadLine()) null null);

               ConsoleWriteLine(Implement other tasks);

               ThreadSleep();

               ConsoleWriteLine(Implement other tasks end );

               ConsoleWriteLine(Get users input);

               ConsoleWriteLine(callerEndInvoke(result));

               ConsoleReadLine();

           }

           delegate string AsyncFuncDelegate(int userInput);

           static string Func(int userInput)

           {

               ConsoleWriteLine(Func start to run);

               ConsoleWriteLine();

               ThreadSleep();

               ConsoleWriteLine(Func end to run);

               return userInputToString();

           }

       }

   }

  輸出結果如下:

  Implement other tasks

  Func start to run

  

  Func end to run

  Implement other tasks end

  Get users input

  

   在當前線程中啟動線程T然後繼續執行當前線程中的其它任務R等待T執行完成當T執行完成後繼續執行當前線程中的其它任務R最後獲取T的返回值

   static void Main()

           {

               AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

               ConsoleWriteLine(Input number please);

               IAsyncResult result = callerBeginInvoke(ConvertToInt(ConsoleReadLine()) null null);

               ConsoleWriteLine(Implement task );

               resultAsyncWaitHandleWaitOne();

               resultAsyncWaitHandleClose();

               ConsoleWriteLine(Implment task );

               ConsoleWriteLine(Get users input);

               ConsoleWriteLine(callerEndInvoke(result));

               ConsoleReadLine();

           }

  輸出結果如下:

  Input number please

  

  Implement task

  Func start to run

  

  Func end to run

  Implment task

  Get users input

  

   在當前線程中啟動線程T只要T在執行就執行任務R最後獲取T的返回值

   [STAThread]

           static void Main()

           {

               AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

               ConsoleWriteLine(Input number please);

               IAsyncResult result = callerBeginInvoke(ConvertToInt(ConsoleReadLine()) null null);

               while (!resultIsCompleted)

               {

                   ThreadSleep();

                   ConsoleWrite(>);

               }

               ConsoleWriteLine();

               ConsoleWriteLine(Implement other task);

               ConsoleWriteLine(Get users input);

               ConsoleWriteLine(callerEndInvoke(result));

               ConsoleReadLine();

           }

  輸出結果如下:

  Func start to run

  

  >>>>>Func end to run

  >

  Implement other task

  Get users input

  

   不需要在當前線程中獲取返回值但是仍然需要對返回值做處理

   using System;

   using SystemCollectionsGeneric;

   using SystemLinq;

   using SystemWindowsForms;

   using SystemThreading;

   using SystemRuntimeRemotingMessaging;

   namespace FirstWF

   {

       static class Program

       {

           /// <summary>

           /// The main entry point for the application

           /// </summary>

           [STAThread]

           static void Main()

           {

               AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

               ConsoleWriteLine(Input number please);

               callerBeginInvoke(ConvertToInt(ConsoleReadLine()) new AsyncCallback(CallBackFunc) Message from Main thread);

               ConsoleWriteLine(Main thread ends);

               ConsoleReadLine();

           }

           delegate string AsyncFuncDelegate(int userInput);

           static string Func(int userInput)

           {

               ConsoleWriteLine(Func start to run);

               ConsoleWriteLine();

               ThreadSleep();

               ConsoleWriteLine(Func end to run);

               return userInputToString();

           }

           static void CallBackFunc(IAsyncResult ar)

           {

               AsyncResult result = ar as AsyncResult;

               string inputMessage = resultAsyncState as string;

               AsyncFuncDelegate caller = resultAsyncDelegate as AsyncFuncDelegate;

               ConsoleWriteLine(call back starts);

               ConsoleWriteLine(inputMessage);

               ConsoleWriteLine(The input number is : + callerEndInvoke(ar));

               ConsoleWriteLine(call back ends);

           }

       }

   }

  輸出結果如下:

  Input number please

  

  Main thread ends

  Func start to run

  

  Func end to run

  call back starts

  Message from Main thread

  The input number is :

  call back ends


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