ArticleDirectory
- 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.
- 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.
- 2. You do not need to obtain the return value in the current thread, but still need to process the return value.
In the past few days, looking at the WF nature, we have mentioned asynchronous processing of. net. Because it usesCodeSegment. So I made up my mind to review asynchronous processing in. net.
I have been using C # for development in. Net for five years. I initially used asynchronous processing in. Net about four years ago. At that time, I did not study the required functions in detail. It is no wonder that WF will become dizzy (the consequence of poor Foundation ).
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
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. toint32 (console. readline (), null, null); console. writeline ("implement other tasks"); thread. sleep (1, 7000); 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 (1, 5000); console. writeline ("func end to run"); Return userinput. tostring ();}}}
The output result is as follows:
Implement other tasks
Func start to run
...
Func end to run
Implement other tasks end...
Get user's 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.
Static void main () {asyncfuncdelegate caller = new asyncfuncdelegate (func); console. writeline ("input number please... "); iasyncresult result = caller. begininvoke (convert. toint32 (console. readline (), null, null); console. writeline ("Implement Task 1"); result. asyncwaithandle. waitone (); result. asyncwaithandle. close (); console. writeline ("implment Task 2"); console. writeline ("Get user's input"); console. writeline (caller. endinvoke (result); console. readline ();}
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 user's 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.
[Stathread] Static void main () {asyncfuncdelegate caller = new asyncfuncdelegate (func); console. writeline ("input number please... "); iasyncresult result = caller. begininvoke (convert. toint32 (console. readline (), null, null); While (! Result. iscompleted) {thread. sleep (1, 1000); console. write (">");} console. writeline (""); console. writeline ("implement other task2"); console. writeline ("Get user's input"); console. writeline (caller. endinvoke (result); console. readline ();}
The output result is as follows:
Func start to run
...
>>>>> Func end to run
>
Implement other task2
Get user's input
23
2. You do not need to obtain the return value in the current thread, but still need to process the return value.
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. toint32 (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 (1, 5000); 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 ");}}}
The output result is as follows:
Input number please...
23
Main thread ends
Func start to run
...
Func end to run
Call back starts
Message from main thread.
The input number is: 23
Call back ends
I remember that the previous Code was not very well written. Although call. begininvoke can start asynchronous calling, almost no endinvoke has been used. Endinvoke can ensure that the asynchronous call ends normally and make the code healthier.
Asynchronous calling can make code execution more efficient, but there should be a healthy habit in asynchronous calling.