First, let's analyze the asynchronous Processing Environment
The returned value needs to be obtained in the current thread.
You do not need to obtain the return value in the current thread, but still need to process the return value.
You can continue to segment the data in 1st.
Start thread T in the current thread, continue to execute other tasks in the current thread, and finally obtain the return value of T in the current thread.
Start thread T in the current thread, continue to execute other tasks R1 in the current thread, and wait until T execution is complete. After T execution is complete, continue to execute other tasks R2 in the current thread, finally, the return value of T is obtained.
Start thread T in the current thread, execute task R as long as T is executed, and finally obtain the return value of T.
Below, I will give examples one by one:
1.1 start thread T in the current thread, continue to execute other tasks in the current thread, and finally obtain the return value of T in the current thread
View sourceprint? 01 using System;
02 using System. Collections. Generic;
03 using System. Linq;
04 using System. Windows. Forms;
05 using System. Threading;
06 using System. Runtime. Remoting. Messaging;
07 namespace FirstWF
08 {
09 static class Program
10 {
11 /// <summary>
12 // The main entry point for the application.
13 /// </summary>
14 [STAThread]
15 static void Main ()
16 {
17 AsyncFuncDelegate caller = new AsyncFuncDelegate (Func );
18 Console. WriteLine ("Input number please ...");
19 IAsyncResult result = caller. BeginInvoke (Convert. ToInt32 (Console. ReadLine (), null, null );
20 Console. WriteLine ("Implement other tasks ");
21 Thread. Sleep (7000 );
22 Console. WriteLine ("Implement other tasks end ...");
23 Console. WriteLine ("Get users input ");
24 Console. WriteLine (caller. EndInvoke (result ));
25 Console. ReadLine ();
26}
27 delegate string AsyncFuncDelegate (int userInput );
28 static string Func (int userInput)
29 {
30 Console. WriteLine ("Func start to run ");
31 Console. WriteLine ("...");
32 Thread. Sleep (5000 );
33 Console. WriteLine ("Func end to run ");
34 return userInput. ToString ();
35}
36}
37}
The output result is as follows:
Implement other tasks
Func start to run
...
Func end to run
Implement other tasks end...
Get users input
56
1.2 start thread T in the current thread, and then continue to execute other tasks R1 in the current thread. Wait until T execution is complete. After T execution is complete, continue to execute other tasks R2 in the current thread, finally, the return value of T is obtained.
View sourceprint? 01 static void Main ()
02 {
03 AsyncFuncDelegate caller = new AsyncFuncDelegate (Func );
04 Console. WriteLine ("Input number please ...");
05 IAsyncResult result = caller. BeginInvoke (Convert. ToInt32 (Console. ReadLine (), null, null );
06 Console. WriteLine ("Implement task 1 ");
07 result. AsyncWaitHandle. WaitOne ();
08 result. AsyncWaitHandle. Close ();
09 Console. WriteLine ("Implment task 2 ");
10 Console. WriteLine ("Get users input ");
11 Console. WriteLine (caller. EndInvoke (result ));
12 Console. ReadLine ();
13}
The output result is as follows:
Input number please...
25
Implement task 1
Func start to run
...
Func end to run
Implment task 2
Get users input
25
1.3 start thread T in the current thread, execute task R as long as T is executed, and finally obtain the return value of T.
View sourceprint? 01 [STAThread]
02 static void Main ()
03 {
04 AsyncFuncDelegate caller = new AsyncFuncDelegate (Func );
05 Console. WriteLine ("Input number please ...");
06 IAsyncResult result = caller. BeginInvoke (Convert. ToInt32 (Console. ReadLine (), null, null );
07 while (! Result. IsCompleted)
08 {
09 Thread. Sleep (1000 );
10 Console. Write ("> ");
11}
12 Console. WriteLine ("");
13 Console. WriteLine ("Implement other task2 ");
14 Console. WriteLine ("Get users input ");
15 Console. WriteLine (caller. EndInvoke (result ));
16 Console. ReadLine (