You can add a sequel to Task . Continuation is a method that will be called when the specified Task completed.
for (int i = 0; i < tsk.Length; i++) { tsk[i] = Task.Factory.StartNew((object obj) => { resp = http.SynchronousRequest(web, 443, true, req); }, i); tsk[i].ContinueWith(antecedent=> {
If you need to complete individual tasks, you will need one stopwatch for each task. You can start StopWatch inside StartNew and stop it in ContinueWith .
If this is your actual code, you can simply time the synchronous operation that you are calling (http.SynchronousRequest in this case). For example, the following code is enough.
for (int i = 0; i < tsk.Length; i++) { tsk[i] = Task.Factory.StartNew((object obj) => { StopWatch watch = StopWatch.StartNew(); resp = http.SynchronousRequest(web, 443, true, req); watch.Stop(); Console.WriteLine(watch.Elapsed); }, i); }
Btw, network operations are inherently asynchronous; An asynchronous API will be available, you can use it instead of wrapping a synchronous web request in Task. For example, perhaps HttpClient.SendAsync .
Sriram sakthivel
source share