【问题标题】:HOW to prevent an ASP.net PageMethod call from blocking other javascript function calls如何防止 ASP.net PageMethod 调用阻止其他 javascript 函数调用
【发布时间】:2016-06-29 00:30:45
【问题描述】:

下面的 JavaScript 函数每 15 秒运行一次,并调用一个 ASP.Net 页面方法。完成大约需要 4 - 8 秒。

我在同一页面上还有另一个 JavaScript 函数,它每 2 秒运行一次,但会定期被前一个方法阻止,需要更长的时间才能完成。

function get_case_list_data(count, max_id) {
   PageMethods.GetCaseList(count, max_id, uid, is_agent, got_case_list_data, OnFailure);
}

请问我们如何防止 ASP.Net 页面方法调用阻止同一页面上的其他 JavaScript 函数执行?

【问题讨论】:

    标签: javascript asp.net asynchronous pagemethods


    【解决方案1】:

    使用浏览器调试工具并检查 PageMethods.GetCaseList 中使用的自动生成代码,然后使用异步 ajax 调用而不是阻塞调用来模拟调用。

    PageMethods 包装器只是为了方便起见,但该代码通常非常难看。您始终可以使用 $.ajax 或本机 XmlHttpRequest 手动调用它。

    异步 ​​= 真;

    如果您进行多次调用,ASP.NET 会话可能会阻塞。在 javascript 方法中使用 alerts 或 console.log 来确定什么是阻塞的

    function get_case_list_data(count, max_id) {
       console.log("before call");
       PageMethods.GetCaseList(count, max_id, uid, is_agent, got_case_list_data, OnFailure);
       console.log("after call");
    }
    
    function got_case_list_data(){
       console.log("in PageMethod success");
       // -- updated --
       // this could be blocking the call to/from other 2 second timer
       // JS is single thread, so window.timeout and ajax callbacks will
       // wait until the function is exited
       // -- end update--
       console.log("end of PageMethod success");
    }
    

    --更新--

    将 asp.net 会话设置为只读删除了将同步线程的独占会话锁

    【讨论】:

    • 所以基本上调用不是异步的?
    • 您可以在 PageMethod 调用之前、之后和成功回调中使用警报进行测试。谷歌“aysnc PageMethods”似乎默认情况下它们可能是aysnc,您需要进行更新以使其同步。无论哪种方式,这个博客都有一些很好的内容可以帮助你入门。 abhijit-j-shetty.blogspot.com/2011/04/…
    • 用 async = true 尝试了 $ajax;结果相同
    • 您提供的链接是用于同步调用的...我认为这是问题所在?
    • 我已经添加了 cosole.log 并删除了所有会话使用 - 没有区别
    猜你喜欢
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    相关资源
    最近更新 更多