I want to be able to measure the time it take to execute an asynchronous method.
When I call the method synchronously, I run the following code and I get the exact amount of time it took a the method MyMethod.
for (int i = 0; i < 5000; i++) {
stopWatches[i].Start();
webService.MyMethod(new Request());
stopWatches[i].Stop();
}
I changed my code to execute MyMethod asynchonously, so now my code looks like this.
var callback = new Action<IAsyncResult>(ar => {
int i = (int)ar.AsyncState;
try {
var response = webService.EndMyMethod(ar);
stopWatches[i].Stop();
}
}
for (int i = 0; i < 5000; i++) {
webService.BeginMyMethod(new Request(), new AsyncCallback(callback), i);
stopWatches[i].Start();
}
The problem is that the stopwatches don't measure the time required to execute a request anymore. They measure the time from when a request entered in the ThreadPool queue until the end of the call. So I end up with a list of stopwatches with a time going up linearly.
How do I fix my stopwatches? Is there a way to know exactly when the request start?
Thanks!
UPDATE: I cannot change the implementation of MyMethod.