Search code examples
javascriptasp.netasynchronouspagemethods

HOW to prevent an ASP.net PageMethod call from blocking other javascript function calls


The JavaScript function below runs every 15 seconds and calls an ASP.Net Page-method. It takes about 4 - 8 seconds to complete.

I also have another JavaScript function on the same page that runs every 2 seconds, but gets periodically blocked by the previous method that takes longer to complete.

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

Please how can we prevent an ASP.Net Page-method call from blocking other JavaScript function execution on the same page?


Solution

  • Use the browser debug tools and inspect the auto-generated code used in PageMethods.GetCaseList then just mimic the call with an async ajax call instead of a blocking one.

    The PageMethods wrapper is just for convenience but that code is normally pretty ugly. You can always manually call it your self with $.ajax or native XmlHttpRequest.

    async = true;

    If your making multiple calls, the ASP.NET session may be doing the blocking. Use alerts or console.log within the javascript methods to determine what is blocking

    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");
    }
    

    -- updated--

    setting the asp.net session to readonly removed the exclusive session lock that would synchronize the threads