c# - Why is using multiple threads not improving performance? -
so i've been trying stuff c# webclient. managed make working program (console application) code similar this:
static void search(string number) { using (var client = new webclient()) { (int = 0; < globalvariable.lenght; a++) { string towrite = "nothing"; (int b = 0; < globalvariable2.lenght; b++) { string result = client.downloadstring(urlstring); //do stuff towrite if page not empty //change towrite , break b loop } console.writeline(towrite); } } } it isn't fast thought can make faster using multiple threads. takes 2 minutes execute.
so tried making loop parallel.for loop. still took 2 minutes execute. read stuff in here , made following code:
static async task awrite(string number, int a) { using (var client = new webclient()) { string towrite = "nothing"; for(int b=0; a<globalvariable2.lenght; b++) { string result = await client.downloadstringtaskasync(uri); //do stuff towrite if page not empty //change towrite , break b loop } console.writeline(towrite); } } and function call it:
private static void asearch(string number) { var tasks = new list<task>(); for(int a=0; a<gobalvariable.length; a++) { tasks.add(awrite(number, a)); } task.waitall(tasks.toarray()); } so thought multiple webclient download strings simultaneously, apparently doesn't happen takes 2 minutes execute. why that? writing in console know not executing in order, still takes same amount of time. how can improve performance of first function through use multiple threads?
you can change http connection limit:
system.net.servicepointmanager.defaultconnectionlimit = 5; check out servicepointmanager.defaultconnectionlimit , article on servicepoint class. property can change default connection limit http connections.
Comments
Post a Comment