c#异步调用的几种方式
首先 我们分析一下异步处理的环境
需要在当前线程中获取返回值
不需要在当前线程中获取返回值 但是仍然需要对返回值做处理
对于第 中情况 还可以继续细分
在当前线程中启动线程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