I have to process different heavy database operations simultaneously. So I decided to make asynchronous calls to a webmethod in my ASP.NET site.
Everything is working fine, except that every call is processed in the same thread, which means its not processing parallel, but serial.
My Webmethod is looking like this:
[WebMethod]
public static string Work(string id)
{
Thread.Sleep(3000);
return DateTime.Now.ToString() + " " + id;
}
In Firebug I can trace the requests. There are 10 requests going out parallel, but the timeline is for example looking like this:
Request01 - 18.34s
Request02 - 09.18s
Request03 - 21.36s
Request04 - 06.14s
Request05 - 27.41s
Request06 - 30.45s
Request07 - 15.26s
Request08 - 03.06s
Request09 - 12.21s
Request10 - 24.40s
I understand, that every request is processed in the same thread. But how can I avoid this? Thank you.
Googling arround and searching Stackoverflow doesn't point me to the following question. But after creating mine its displayed in the sidebar.
So my question is a duplicate of this: asp.net call WebMethod from Javascript asyncronous
I had to make the EnableSessionState
Attribute in the @Page
Directive to ReadOnly
, because it isn't threadsafe.