c# – Ajax回发导致所有其他Web请求挂起,直到方法请求完成
发布时间:2020-12-15 17:21:47 所属栏目:百科 来源:网络整理
导读:编辑 好的,这是典型的,我在寻求帮助后找到问题的可能解决方案! 我的代码现在使用Threading来生成一个新线程,以独立于当前请求执行索引.它似乎工作. 我提出的代码: private static WebDocument Document;private static readonly object Locker = new objec
编辑
好的,这是典型的,我在寻求帮助后找到问题的可能解决方案! 我的代码现在使用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(); } } 谢谢 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |