Primitive user-mode construction--an asynchronous Web request instance implemented by interlocking constructs interlocked

Source: Internet
Author: User

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Net.Http;5 usingSystem.Text;6 usingSystem.Threading;7 usingSystem.Threading.Tasks;8 9 namespaceTestTen { One     enumCoordinationstatus A     { - Alldone, - Cancel, the Timeout -     } -     classAsynccoordinator -     { +         Private intstatusreported =0; -         Private intOp_count =1; +  A         PrivateAction<coordinationstatus>callback; at         Privatetimer timer; -  -          Public voidAbouttobegin (intnum =1) -         { -Interlocked.add (refop_count, num); -         } in  -          Public voidjusttoend () to         { +             if(Interlocked.decrement (refOp_count) = =0) -             { the Reportstatus (coordinationstatus.alldone); *             } $         }Panax Notoginseng  -          Public voidAllbegun (Action<coordinationstatus> callback,intTimeout =timeout.infinite) the         { +              This. Callback =callback; A             if(Timeout! =timeout.infinite) the             { +Timer =NewTimer (Expired,NULL, timeout, timeout.infinite); -             } $ justtoend (); $         } -  -         Private voidExpired (Objectobj) the         { - Reportstatus (coordinationstatus.cancel);Wuyi         } the          Public voidCancel () -         { Wu Reportstatus (coordinationstatus.cancel); -         } About         Private voidreportstatus (coordinationstatus status) $         { -             if(Interlocked.exchange (refStatusreported,1) ==0) -             { - callback (status); A             } +         } the  -  $  the     } the  the     classmultiwebrequests the     { -         PrivateAsynccoordinator coordinator =Newasynccoordinator (); in  the         Privatedictionary<string,Object> Servers =Newdictionary<string,Object>(){ the{"http://www.baidu.com",NULL}, About{"http://www.sina.com",NULL}, the{"http://www.qq.com",NULL}, the         }; the  +          Publicmultiwebrequests () -         { the Bayi             varHTTP =NewHttpClient (); the             foreach(varUrlinchservers. Keys) the             { -                 //a request was sent -Coordinator. Abouttobegin (1); thehttp. Getbytearrayasync (URL). ContinueWith (Task =getresult (URL, Task)); the             } the             //all requests are sent . the coordinator. Allbegun (Alldone, timeout.infinite); -         } the  the         Private voidGetResult (stringServer, task<byte[]>Task) the         {94             ObjectRes; the             if(Task. Exception! =NULL) the             { theres =task. exception.innerexceptions;98             } About             Else -             {101res =task. Result.length;102             }103Servers[server] =Res;104             //a request was completed the coordinator. Justtoend ();106         }107 108          Public voidCancel ()109         { the coordinator. Cancel ();111         } the 113         Private voidalldone (coordinationstatus status) the         { the             Switch(status) the             {117                  CaseCoordinationstatus.alldone:118Console.WriteLine ("alldone:");119  -                     foreach(varIteminchservers)121                     {122 Console.Write (item. Key);123                         Objectval =item. Value;124                         if(Val isException) the                         {126Console.WriteLine ("Exception: {0}", Val. GetType (). Name);127                         } -                         Else129                         { theConsole.WriteLine ("returned {0:n0} bytes", Val);131                         } the                     }133                      Break;134                  CaseCoordinationstatus.cancel:135                      Break;136                  Casecoordinationstatus.timeout:137                      Break;138                 default:139                      Break; $             }141         }142     }143}

Primitive user-mode construction--an asynchronous Web request instance implemented by interlocking constructs interlocked

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.