These days, see WF essence, which refers to the asynchronous processing of. Net. Because the inside uses is the code fragment, therefore a bit does not understand. So I made up my mind to brush up on asynchronous processing in. Net.
Using C # has been in. NET development for more than 5 years, and the initial use of asynchronous processing in. NET was approximately 4 years ago. At that time, just to achieve the requirements of the function, there is no detailed study. It is no wonder that WF will be dizzy (the basis of the consequences of the prison).
First, we analyze the asynchronous processing environment
1. Need to get the return value in the current thread
2. You do not need to get the return value in the current thread, but you still need to handle the return value
For the 1th situation, you can also continue to subdivide
1. Start the thread T in the current thread, then continue with the other tasks in the current thread, and finally get the return value of T in the current thread
2. Start the thread T in the current thread, then continue with the other task R1 in the current thread, wait for t execution, and when T completes, continue with the other tasks in the current thread R2, and finally get the return value of T
3. Start the thread T in the current thread, as long as T executes the task R at execution, and finally gets the return value of T
Here, I'll give you an example:
1.1 Starts the thread T in the current thread, then continues with the other tasks in the current thread, and finally gets the return value of T in the current thread
The
is 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>
12 The main entry point is for the application.
///</summary>
[STAThread]
static void Main ()
{
17 Asyncfuncdelegate caller = new Asyncfuncdelegate (FUNC);
Console.WriteLine ("Input number please ...");
IAsyncResult result = caller. BeginInvoke (Convert.ToInt32 (Console.ReadLine ()), NULL, NULL);
Console.WriteLine ("Implement other Tasks");
Thread.Sleep (7000);
Console.WriteLine ("Implement Other tasks End ...");
Console.WriteLine ("Get user ' s input");
ConsoLe. WriteLine (caller. EndInvoke (result));
Console.ReadLine ();
26}
Delegate string asyncfuncdelegate (int userinput);
static string Func (int userinput)
{
Console.WriteLine ("Func start to run ");
Console.WriteLine ("...");
Thread.Sleep (5000);
Console.WriteLine ("Func End to Run");
return userinput.tostring ();
35}
36}
Notoginseng}
The output results are as follows:
Implement other tasks
Func start to run
...
Func end to run
Implement other tasks end ...
Get user's input
56