Rollcredit
Rollcredit

Reputation: 453

Ajax post back causing all other web requests to hang until the method request finishes

Edit

Ok thats typical, I figure out a possible solution to the problem just after I ask for help!

My code is now using Threading to spawn a new thread to perform the indexing independently from the current request. It seems to be working.

The code that I came up with:

private static WebDocument Document;
private static readonly object Locker = new object();

[WebMethod(true)]
public static string Index(string uri)
{
  WebDocument document = WebDocument.Get(uri);

  if (document == null)
    document = WebDocument.Create(uri);

  Document = document;
  Thread thread = new Thread(IndexThisPage);
  thread.Start();

  //document.Index();

  return "OK";
}

public static void IndexThisPage()
{
  lock (Locker)
  {
    Document.Index();
  }
}

Orginal Question

On all of my pages I have an ajax post that performs an index on the current page as well as all documents on the page. The indexer I'm using is Keyoti.

What seems to happen is when one page is being indexed any request to any other page seems to not respond (i.e. its stuck on "Waiting for server") until the indexing is finished. Note: I'm loading the different pages from the same machine, because the code is local.

Here's the ajax I'm using:

<script type="text/javascript" src="/Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
  $(window).load(function () {
    $.ajax({
      type: "POST",
      url: "/DoIndex.aspx/Index",
      data: "{ uri: '" + self.location + "' }",
      contentType: "application/json; charset=utf-8",
      dataType: "json"
    });
  });
</script>

And the method it calls:

[WebMethod(true)]
public static string Index(string uri)
{
  WebDocument document = WebDocument.Get(uri);

  if (document == null)
    document = WebDocument.Create(uri);

  document.Index();

  return "OK";
}

Anyone have any thoughts?

Upvotes: 4

Views: 648

Answers (1)

Harry
Harry

Reputation: 91

your answer is completely right. If you using .Net 4, I'd like to let you know you could use task instead of thread. I guess it's easer to read and also it'll let the OS to decide how manage threads.

this is the good explanation as well.

private static WebDocument Document;
private static readonly object Locker = new object();

[WebMethod(true)]
public static string Index(string uri)
{
  WebDocument document = WebDocument.Get(uri);

  if (document == null)
    document = WebDocument.Create(uri);

  Document = document;

  // start a new background thread
  var System.Threading.Tasks.task = Task.Factory.StartNew(() => IndexThisPage);

  //document.Index();

  return "OK";
}

public static void IndexThisPage()
{
  lock (Locker)
  {
    Document.Index();
  }
}

Thanks

Upvotes: 2

Related Questions