c#异步调用的几种方式

 我来答
科创17
2022-11-20 · TA获得超过5893个赞
知道小有建树答主
回答量:2846
采纳率:100%
帮助的人:173万
展开全部

  首先 我们分析一下异步处理的环境

  需要在当前线程中获取返回值

  不需要在当前线程中获取返回值 但是仍然需要对返回值做处理

  对于第 中情况 还可以继续细分

  在当前线程中启动线程T 然后继续执行当前线程中的其它任务 最后在当前线程中获取T的返回值

  在当前线程中启动线程T 然后继续执行当前线程中的其它任务R 等待T执行完成 当T执行完成后 继续执行当前线程中的其它任务R 最后获取T的返回值

  在当前线程中启动线程T 只要T在执行就执行任务R 最后获取T的返回值

  下面 我将一一给出例子

   在当前线程中启动线程T 然后继续执行当前线程中的其它任务 最后在当前线程中获取T的返回值

   using System;

   using System Collections Generic;

   using System Linq;

   using System Windows Forms;

   using System Threading;

   using System Runtime Remoting Messaging;

   namespace FirstWF

   {

        static class Program

        {

            /// <summary>

            /// The main entry point for the application

            /// </summary>

            [STAThread]

            static void Main()

            {

                AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

                Console WriteLine( Input number please );

                IAsyncResult result = caller BeginInvoke(Convert ToInt (Console ReadLine()) null null);

                Console WriteLine( Implement other tasks );

                Thread Sleep( );

                Console WriteLine( Implement other tasks end );

                Console WriteLine( Get user s input );

                Console WriteLine(caller EndInvoke(result));

                Console ReadLine();

            }

            delegate string AsyncFuncDelegate(int userInput);

            static string Func(int userInput)

            {

                Console WriteLine( Func start to run );

                Console WriteLine( );

                Thread Sleep( );

                Console WriteLine( Func end to run );

                return userInput ToString();

            }

        }

   }

  输出结果如下:

  Implement other tasks

  Func start to run

  

  Func end to run

  Implement other tasks end

  Get user s input

  

   在当前线程中启动线程T 然后继续执行当前线程中的其它任务R 等待T执行完成 当T执行完成后 继续执行当前线程中的其它任务R 最后获取T的返回值

   static void Main()

            {

                AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

                Console WriteLine( Input number please );

                IAsyncResult result = caller BeginInvoke(Convert ToInt (Console ReadLine()) null null);

                Console WriteLine( Implement task );

                result AsyncWaitHandle WaitOne();

                result AsyncWaitHandle Close();

                Console WriteLine( Implment task );

                Console WriteLine( Get user s input );

                Console WriteLine(caller EndInvoke(result));

                Console ReadLine();

            }

  输出结果如下:

  Input number please

  

  Implement task

  Func start to run

  

  Func end to run

  Implment task

  Get user s input

  

   在当前线程中启动线程T 只要T在执行就执行任务R 最后获取T的返回值

   [STAThread]

            static void Main()

            {

                AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

                Console WriteLine( Input number please );

                IAsyncResult result = caller BeginInvoke(Convert ToInt (Console ReadLine()) null null);

                while (!result IsCompleted)

                {

                    Thread Sleep( );

                    Console Write( > );

                }

                Console WriteLine( );

                Console WriteLine( Implement other task );

                Console WriteLine( Get user s input );

                Console WriteLine(caller EndInvoke(result));

                Console ReadLine();

            }

  输出结果如下:

  Func start to run

  

  >>>>>Func end to run

  >

  Implement other task

  Get user s input

  

   不需要在当前线程中获取返回值 但是仍然需要对返回值做处理

   using System;

   using System Collections Generic;

   using System Linq;

   using System Windows Forms;

   using System Threading;

   using System Runtime Remoting Messaging;

   namespace FirstWF

   {

        static class Program

        {

            /// <summary>

            /// The main entry point for the application

            /// </summary>

            [STAThread]

            static void Main()

            {

                AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

                Console WriteLine( Input number please );

                caller BeginInvoke(Convert ToInt (Console ReadLine()) new AsyncCallback(CallBackFunc) Message from Main thread );

                Console WriteLine( Main thread ends );

                Console ReadLine();

            }

            delegate string AsyncFuncDelegate(int userInput);

            static string Func(int userInput)

            {

                Console WriteLine( Func start to run );

                Console WriteLine( );

                Thread Sleep( );

                Console WriteLine( Func end to run );

                return userInput ToString();

            }

            static void CallBackFunc(IAsyncResult ar)

            {

                AsyncResult result = ar as AsyncResult;

                string inputMessage = result AsyncState as string;

                AsyncFuncDelegate caller = result AsyncDelegate as AsyncFuncDelegate;

                Console WriteLine( call back starts );

                Console WriteLine(inputMessage);

                Console WriteLine( The input number is : + caller EndInvoke(ar));

                Console WriteLine( 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 :

lishixinzhi/Article/program/net/201311/13035

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式