4

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?

Rollcredit
  • 453
  • 2
  • 5
  • 13
  • Is Document.Index doing something that requires it being locked other than you're storing the document in a static variable? You may be better off passing the Document instance directly to the IndexThisPage method instead. – rossisdead Nov 17 '11 at 04:11
  • Yeah, its using keyoti's internal indexing engine to open up the indexing directory and modify some flat files. – Rollcredit Nov 18 '11 at 00:25

1 Answers1

2

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

Community
  • 1
  • 1
Harry
  • 91
  • 4