c# - Sending multiple requests to a server using multithreading -
c# - Sending multiple requests to a server using multithreading -
i have task form thousands of requests later sent server. server returns response each request , response dumped output file line line.
the pseudo code goes this:
//requests contains thousands of requests sent server string[] requests = getrequestsstring(); foreach(string request in requests) { string response = makewebrequest(request); parseanddump(response); }
now, can seen serve handling requests 1 one. want create entire process fast. server in question capable of handling multiple requests @ time. want apply multi-threading , send let's 4 requests server @ time , dump response in same thread.
can please give me pointer possible approaches.
you can take advantage of task
.net 4.0 , new toy httpclient
, sample code below showed how send requests in parallel, dump response in same thread using continuewith
:
var httpclient = new httpclient(); var tasks = requests.select(r => httpclient.getstringasync(r).continuewith(t => { parseanddump(t.result); }));
task uses threadpool
under hood, don't need specify how many threads should used, threadpool
manage in optimized way.
c# multithreading
Comments
Post a Comment