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