asynchronous - Measuring request time in async requests -
i need run multiple web requests , measure time between request , corresponding response.
both solutions existing code using backgroundworker , new code using tpl library have following issue cannot understand:
var watch = stopwatch.startnew(); queue.enqueue(task.factory .fromasync<webresponse>(webrequest.begingetresponse, webrequest.endgetresponse, null) .continuewith(task => { using (var response = (httpwebresponse)task.result) { // stopwatch shows wrong results // after multiple request watch.elapsed time arrives 10 seconds // wrong // same code running in console application works corectly debug.writeline("{0}\t{1}\t{2}", response.statuscode, response.contenttype, watch.elapsed); // lines in production wiforms app if (eventwebrequestfinished != null) { // update winform gui elements (add listboc) } } },
the same issue have using datetime.now
method.
if it's code, without static variables , etc., cause of problem may closure of variable 'watch'. expression gets first value of variable , next times uses value. try send variable value via 3rd parameter 'state' of fromasync method.
Comments
Post a Comment