Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Asynchronous webmethods

Asynchronous webmethods

Scheduled Pinned Locked Moved ASP.NET
helpcsharpjavajavascriptasp-net
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    david vermeulen
    wrote on last edited by
    #1

    Hi, I am fairly new to asp.net and need a bit of help please. In our project we are doing a callback from the client-side to the server side using a webmethod in order to try and keep the page request alive. From what I can see in Firebug is that, on the server side, the webmethod is triggered successfully from the javascript until I start with my async event on the server side, then the webmethod waits for it to be finished before the javascript calls the pagemethod again. However this process of ours can take anything from 1 minute to 40 minutes, so it is quiet essential that I keep the page request alive. I don't want to increase the timeout on the scriptmanager since I don't think that is the right way to approach this issue. Here is a small sample of how the code code is: Java script:

    function SimpleCallback() {
    var lCurrentStatus = PageMethods.GetCurrentStatus(ProcessResults);
    }

    function ProcessResults(pCurrentStatus) {
    if (pCurrentStatus != "Completed") {
    window.setTimeout("SimpleCallback()", 100);
    }
    }

    function SaveButtonClick(pArgument) {
    var lAjaxManager = $find("<%= MyAjaxManager.ClientId %>");
    lAjaxManager.ajaxRequest(pArgument);
    SimpleCallback();
    }

    <asp:Button ID="SaveButton" runat="server" Text="Save" OnClientClick="return SaveButtonClick('SaveStuff');"

    Server side code:

    [WebMethod]
    public static string GetCurrentStatus()
    {
    return FCurrentStatus;
    }

    protected void MyAjaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
    if (e.Argument == "SaveStuff")
    {
    FCurrentStatus = "ProcessRunning";
    SaveInformation();
    }
    }

    //While this is being executed the webmetod, GetCurrentStatus, is ignored until this process is completed
    delegate void MyLongRunningProcess();

    private void SaveInformation()
    {
    var lLongRunningMethod = new MyLongRunningProcess(NextClass.Save());

    IAsyncResult lResult = lLongRunningMethod.BeginInvoke(null, null);

    while (!lResult.IsCompleted)
    {
    //Update progressbar with new percentage
    }

    lLongRunningMethod.EndInvoke(lResult);
    }

    Is there a way of doing the pagemethod call from javascript asynchronously, since it seems this is my problem. Or is there another way of approaching this in order to keep the page request alive? Thank you in advance, David.

    N N 2 Replies Last reply
    0
    • D david vermeulen

      Hi, I am fairly new to asp.net and need a bit of help please. In our project we are doing a callback from the client-side to the server side using a webmethod in order to try and keep the page request alive. From what I can see in Firebug is that, on the server side, the webmethod is triggered successfully from the javascript until I start with my async event on the server side, then the webmethod waits for it to be finished before the javascript calls the pagemethod again. However this process of ours can take anything from 1 minute to 40 minutes, so it is quiet essential that I keep the page request alive. I don't want to increase the timeout on the scriptmanager since I don't think that is the right way to approach this issue. Here is a small sample of how the code code is: Java script:

      function SimpleCallback() {
      var lCurrentStatus = PageMethods.GetCurrentStatus(ProcessResults);
      }

      function ProcessResults(pCurrentStatus) {
      if (pCurrentStatus != "Completed") {
      window.setTimeout("SimpleCallback()", 100);
      }
      }

      function SaveButtonClick(pArgument) {
      var lAjaxManager = $find("<%= MyAjaxManager.ClientId %>");
      lAjaxManager.ajaxRequest(pArgument);
      SimpleCallback();
      }

      <asp:Button ID="SaveButton" runat="server" Text="Save" OnClientClick="return SaveButtonClick('SaveStuff');"

      Server side code:

      [WebMethod]
      public static string GetCurrentStatus()
      {
      return FCurrentStatus;
      }

      protected void MyAjaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)
      {
      if (e.Argument == "SaveStuff")
      {
      FCurrentStatus = "ProcessRunning";
      SaveInformation();
      }
      }

      //While this is being executed the webmetod, GetCurrentStatus, is ignored until this process is completed
      delegate void MyLongRunningProcess();

      private void SaveInformation()
      {
      var lLongRunningMethod = new MyLongRunningProcess(NextClass.Save());

      IAsyncResult lResult = lLongRunningMethod.BeginInvoke(null, null);

      while (!lResult.IsCompleted)
      {
      //Update progressbar with new percentage
      }

      lLongRunningMethod.EndInvoke(lResult);
      }

      Is there a way of doing the pagemethod call from javascript asynchronously, since it seems this is my problem. Or is there another way of approaching this in order to keep the page request alive? Thank you in advance, David.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Calling the pagemethod is essentially asynchronous since it is out of band and the user can continue to interact with the page. Typically one places some type of progress indicator on the page while waiting for the out of band call to complete. Look here[^] for an idea


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      • D david vermeulen

        Hi, I am fairly new to asp.net and need a bit of help please. In our project we are doing a callback from the client-side to the server side using a webmethod in order to try and keep the page request alive. From what I can see in Firebug is that, on the server side, the webmethod is triggered successfully from the javascript until I start with my async event on the server side, then the webmethod waits for it to be finished before the javascript calls the pagemethod again. However this process of ours can take anything from 1 minute to 40 minutes, so it is quiet essential that I keep the page request alive. I don't want to increase the timeout on the scriptmanager since I don't think that is the right way to approach this issue. Here is a small sample of how the code code is: Java script:

        function SimpleCallback() {
        var lCurrentStatus = PageMethods.GetCurrentStatus(ProcessResults);
        }

        function ProcessResults(pCurrentStatus) {
        if (pCurrentStatus != "Completed") {
        window.setTimeout("SimpleCallback()", 100);
        }
        }

        function SaveButtonClick(pArgument) {
        var lAjaxManager = $find("<%= MyAjaxManager.ClientId %>");
        lAjaxManager.ajaxRequest(pArgument);
        SimpleCallback();
        }

        <asp:Button ID="SaveButton" runat="server" Text="Save" OnClientClick="return SaveButtonClick('SaveStuff');"

        Server side code:

        [WebMethod]
        public static string GetCurrentStatus()
        {
        return FCurrentStatus;
        }

        protected void MyAjaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
        if (e.Argument == "SaveStuff")
        {
        FCurrentStatus = "ProcessRunning";
        SaveInformation();
        }
        }

        //While this is being executed the webmetod, GetCurrentStatus, is ignored until this process is completed
        delegate void MyLongRunningProcess();

        private void SaveInformation()
        {
        var lLongRunningMethod = new MyLongRunningProcess(NextClass.Save());

        IAsyncResult lResult = lLongRunningMethod.BeginInvoke(null, null);

        while (!lResult.IsCompleted)
        {
        //Update progressbar with new percentage
        }

        lLongRunningMethod.EndInvoke(lResult);
        }

        Is there a way of doing the pagemethod call from javascript asynchronously, since it seems this is my problem. Or is there another way of approaching this in order to keep the page request alive? Thank you in advance, David.

        N Offline
        N Offline
        NeverHeardOfMe
        wrote on last edited by
        #3

        40 minutes?! Surely you don't expect the user to hang around while that happens...? It doens't matter what code you use if they simply shut their computer down... ..on the assumption that all that matters is that your long running process is initiated by them, but they don;t need to wait for the result (you could always show it to them next time they log on), then you can accomplish this by calling a new HttpWebRequest, whcih will run in a new thread and won't be affected by what the user does once it is invoked. You will, by the sound of it, need to set/increase the ScriptTimeout property of your long running process. Place your long running process in a generivc handler ("longrunning.ashx"), then call this from your server-side code as follows:

        Dim rq As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create("longrunning.ashx"), System.Net.HttpWebRequest)
        rq.Method = "POST"
        rq.ContentType = "application/x-www-form-urlencoded"
        Dim postData As String = "" ' any data that needs to be passed to the handler
        rq.ContentLength = postData.Length
        Dim writer As New System.IO.StreamWriter(rq.GetRequestStream(), System.Text.Encoding.ASCII)
        writer.Write(postData)
        writer.Close()

        Note that you should specify the full URL (http path) to "longrunning.ashx", not just the handler filename as I have above. I can only giove you VB code for this I'm afraid - after that you;re on your own...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups