In the past few days, looking at the WF nature, we have mentioned asynchronous processing of. net. Because it uses code snippets, it cannot be understood. 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:
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; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. windows. forms; <br/> using system. threading; <br/> using system. runtime. remoting. messaging; <br/> namespace firstwf <br/> {<br/> static class Program <br/> {<br/> /// <summary> <br/> /// the main entry point for the application. <br/> // </Summary> <br/> [stathread] <br/> static void main () <br/>{< br/> asyncfuncdelegate caller = new asyncfuncdelegate (func); <br/> console. writeline ("input number please... "); <br/> iasyncresult result = caller. begininvoke (convert. toint32 (console. readline (), null, null); <br/> console. writeline ("implement other tasks"); <br/> thread. sleep (7000); <br/> console. writeline ("implement other tasks end... "); <br/> console. writeline ("Get user's input"); <br/> console. writeline (caller. endinvoke (result); <br/> console. readline (); <br/>}< br/> delegate string asyncfuncdelegate (INT userinput); <br/> static string func (INT userinput) <br/> {<br/> console. writeline ("func start to run"); <br/> console. writeline ("... "); <br/> thread. sleep (5000); <br/> console. writeline ("func end to run"); <br/> return userinput. tostring (); <br/>}< br/>}
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
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.
Static void main () <br/>{< br/> asyncfuncdelegate caller = new asyncfuncdelegate (func); <br/> console. writeline ("input number please... "); <br/> iasyncresult result = caller. begininvoke (convert. toint32 (console. readline (), null, null); <br/> console. writeline ("Implement Task 1"); <br/> result. asyncwaithandle. waitone (); <br/> result. asyncwaithandle. close (); <br/> console. writeline ("implment Task 2"); <br/> console. writeline ("Get user's input"); <br/> console. writeline (<MCE: Script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js <MCE: script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"> </MCE: SCRIPT> <MCE: script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"> </MCE: SCRIPT> "mce_src =" http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js "> </MCE: SCRIPT> <MCE: Script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"> </MCE: SCRIPT> caller. endinvoke (result); <br/> console. readline (); <br/>}
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
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] <br/> static void main () <br/> {<br/> asyncfuncdelegate caller = new asyncfuncdelegate (func); <br/> console. writeline ("input number please... "); <br/> iasyncresult result = caller. begininvoke (convert. toint32 (console. readline (), null, null); <br/> while (! Result. iscompleted) <br/>{< br/> thread. sleep (1000); <br/> console. write (">"); <br/>}< br/> console. writeline (""); <br/> console. writeline ("implement other task2"); <br/> console. writeline ("Get user's input"); <br/> console. writeline (caller. endinvoke (result); <br/> console. readline (); <br/>}
The output result is as follows:
Func start to run
...
>>>>> Func end to run
>
Implement other task2
Get user's input
23
You do not need to obtain the return value in the current thread, but still need to process the return value.
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. windows. forms; <br/> using system. threading; <br/> using system. runtime. remoting. messaging; <br/> namespace firstwf <br/> {<br/> static class Program <br/> {<br/> /// <summary> <br/> /// the main entry point for the application. <br/> // </Summary> <br/> [stathread] <br/> static void main () <br/>{< br/> asyncfuncdelegate caller = new asyncfuncdelegate (func); <br/> console. writeline ("input number please... "); <br/> caller. begininvoke (convert. toint32 (console. readline (), new asynccallback (callbackfunc), "Message from main thread. "); <br/> console. writeline ("main thread ends"); <br/> console. readline (); <br/>}< br/> delegate string asyncfuncdelegate (INT userinput); <br/> static string func (INT userinput) <br/> {<br/> console. writeline ("func start to run"); <br/> console. writeline ("... "); <br/> thread. sleep (5000); <br/> console. writeline ("func end to run"); <br/> return userinput. tostring (); <br/>}< br/> static void callbackfunc (iasyncresult AR) <br/>{< br/> asyncresult result = Ar as asyncresult; <br/> string inputmessage = result. asyncstate as string; <br/> asyncfuncdelegate caller = result. asyncdelegate as asyncfuncdelegate; <br/> console. writeline ("call back starts"); <br/> console. writeline (inputmessage); <br/> console. writeline ("the input number is:" + caller. endinvoke (AR); <br/> console. writeline ("call back ends"); <br/>}< br/>
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