1

I have the following method in my GenericHandler.ashx:

private void T1()
{
    System.Threading.Thread.Sleep(2000);
    Ctx.Response.Write(Serializer.Serialize(new { foo = "bar" }));
}

If I make 2 $.ajax-calls to the handler simultaneously, I will get the first response after 2 seconds, and the next after 4.

Is there any way to make my GenericHandler.ashx handle both ajax calls simultaneously?

jbl
  • 15,179
  • 3
  • 34
  • 101
Johan
  • 35,120
  • 54
  • 178
  • 293

3 Answers3

2

What you seem to need is a Asynchronous HTTP Handler

During processing of an asynchronous HTTP handler, ASP.NET puts the thread that would ordinarily be used for the external process back into the thread pool until the handler receives a callback from the external process. This can prevent thread blocking and improve performance, because only a limited number of threads can be executing at the same time. If many users request synchronous HTTP handlers that rely on external processes, the operating system can quickly run out of threads because many threads are blocked and waiting for an external process.

You current method's body would be located in the StartAsyncTask method body of the provided example

Pankaj
  • 9,749
  • 32
  • 139
  • 283
jbl
  • 15,179
  • 3
  • 34
  • 101
0

You need to load the jquery.unobtrusive-ajax once during your session. Check whether it's already loaded:

if (Request.Headers["X-Requested-With"] != "XMLHttpRequest")
{
    Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js");
}
Azhar Khorasany
  • 2,712
  • 16
  • 20
  • What exactly would the script do? – Johan Sep 02 '13 at 13:18
  • Checks whether the requested header has already rendered the unobtrusive jquery file or not. Look at this http://stackoverflow.com/questions/11326792/asp-net-mvc-3-and-jquery-unobtrusive-ajax-min-js – Azhar Khorasany Sep 02 '13 at 13:30
0

Without seeing your parameters for $.ajax, I'm guessing your need to set async to true.

$.ajax({
   url: "page.ashx",
   async: true
});

Also, please be aware different browsers react differently dependant on how many requests it allows at once.

Check out async.

Papa
  • 1,628
  • 1
  • 11
  • 16
  • Why would I want to make a synchronous call? The whole point with $.ajax is to make use of the async part. – Johan Sep 02 '13 at 13:18
  • Are you calling a method with a single ajax call twice or is it two code blocks one after the other in one method? I think the behaviour could be cause it waits for the method to complete. Please see: http://stackoverflow.com/a/4428894/2637902 – Papa Sep 02 '13 at 13:48