编辑
好的,这是典型的,我在寻求帮助后找到问题的可能解决方案!
我的代码现在使用Threading来生成一个新线程,以独立于当前请求执行索引.它似乎工作.
我提出的代码:
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";
}
有人有什么想法?
解决方法:
你的答案是完全正确的.
如果您使用.Net 4,我想告诉您可以使用任务而不是线程.
我想它更容易阅读,它也会让操作系统决定如何管理线程.
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();
}
}
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。