Ajax post-back导致所有其他web请求挂起,直到方法请求完成
本文关键字:请求 挂起 方法 web post-back 其他 Ajax | 更新日期: 2023-09-27 18:14:48
编辑
好吧,这是典型的,我在寻求帮助后就想出了一个可能的解决方案!
我的代码现在使用线程生成一个新线程,以便独立于当前请求执行索引。它似乎在起作用。
我想出的代码:
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();
}
}
原始问题
在我的所有页面上,我都有一个ajax帖子,它对当前页面以及页面上的所有文档执行索引。我使用的索引器是Keyoti。
似乎发生的情况是,当一个页面被索引时,对任何其他页面的任何请求似乎都不会响应(即,它被困在"等待服务器"上(,直到索引完成。注意:我从同一台机器加载不同的页面,因为代码是本地的。
这是我正在使用的ajax:
<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>
以及它调用的方法:
[WebMethod(true)]
public static string Index(string uri)
{
WebDocument document = WebDocument.Get(uri);
if (document == null)
document = WebDocument.Create(uri);
document.Index();
return "OK";
}
有人有什么想法吗?
您的答案完全正确。如果您使用。Net4,我想让你知道你可以使用任务而不是线程。我想它更容易阅读,而且它可以让操作系统决定如何管理线程。
这也是一个很好的解释。
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();
}
}
感谢