Async method raised in web page doesn't refresh page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TS

    Async method raised in web page doesn't refresh page

    Im in a web page and call an asynchronous method in business class. the call
    back method is in the web page.

    When page processes, it runs thru code begins invoking the method then the
    page unloads. When the callback method is raised, only the method in the web
    page is run and the page never refreshes, it seems it all happens on the
    server side.

    I am trying to refresh the constrols on the page inside the callback method,
    but when id do, it isn't flushed to the browser.

    Anyone know how i can do this with this long running async thread?

    thanks a bunch!


  • Marina

    #2
    Re: Async method raised in web page doesn't refresh page

    Right, it happens asynchronously. By the time it does a call back, the page
    has no doubt finished processing, sent the results back to the client, and
    has been displayed in the browser. The request is finished, the connection
    is over.


    "TS" <manofsteele1@n ospam.nospam> wrote in message
    news:OF%23uscfk FHA.2472@TK2MSF TNGP15.phx.gbl. ..[color=blue]
    > Im in a web page and call an asynchronous method in business class. the
    > call
    > back method is in the web page.
    >
    > When page processes, it runs thru code begins invoking the method then the
    > page unloads. When the callback method is raised, only the method in the
    > web
    > page is run and the page never refreshes, it seems it all happens on the
    > server side.
    >
    > I am trying to refresh the constrols on the page inside the callback
    > method,
    > but when id do, it isn't flushed to the browser.
    >
    > Anyone know how i can do this with this long running async thread?
    >
    > thanks a bunch!
    >
    >[/color]


    Comment

    • Brock Allen

      #3
      Re: Async method raised in web page doesn't refresh page

      It's because the page has already completed rendering to the browser by the
      time the aserver async event is raised. You need to have the page wait around
      until the async work is done before it completes its rendering. Are you working
      in 1.1 or 2.0? Fritz has an article on how to achieve this in v1.1:

      Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.


      In 2.0 this is somewhat better integrated at the page level with the Async
      Page feature.

      -Brock
      DevelopMentor



      [color=blue]
      > Im in a web page and call an asynchronous method in business class.
      > the call back method is in the web page.
      >
      > When page processes, it runs thru code begins invoking the method then
      > the page unloads. When the callback method is raised, only the method
      > in the web page is run and the page never refreshes, it seems it all
      > happens on the server side.
      >
      > I am trying to refresh the constrols on the page inside the callback
      > method, but when id do, it isn't flushed to the browser.
      >
      > Anyone know how i can do this with this long running async thread?
      >
      > thanks a bunch!
      >[/color]



      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Async method raised in web page doesn't refresh page

        TS,

        You can't perform asynchronous operations on a web server like this and
        expect the page to be refreshed accordingly. You will need to have your
        operation complete, and then store the results of that operation on the
        server.

        Then, on the client side, you will have to poll some page over and over
        again until the operation completes. When it does, you can get the results
        and post them on the page accordingly.

        You might also want to look into using XML on the page itself,
        specifically, using the XML parser in javascript to load your data, and
        update using dynamic HTML.

        Hope this helps.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "TS" <manofsteele1@n ospam.nospam> wrote in message
        news:OF%23uscfk FHA.2472@TK2MSF TNGP15.phx.gbl. ..[color=blue]
        > Im in a web page and call an asynchronous method in business class. the
        > call
        > back method is in the web page.
        >
        > When page processes, it runs thru code begins invoking the method then the
        > page unloads. When the callback method is raised, only the method in the
        > web
        > page is run and the page never refreshes, it seems it all happens on the
        > server side.
        >
        > I am trying to refresh the constrols on the page inside the callback
        > method,
        > but when id do, it isn't flushed to the browser.
        >
        > Anyone know how i can do this with this long running async thread?
        >
        > thanks a bunch!
        >
        >[/color]


        Comment

        • Wessel Troost

          #5
          Re: Async method raised in web page doesn't refresh page

          > Then, on the client side, you will have to poll some page over and[color=blue]
          > over
          > again until the operation completes. When it does, you can get the
          > results
          > and post them on the page accordingly.
          >[/color]
          The server side code can ask the client to refresh like this:

          // Ask client to refresh every 3 seconds
          Response.Append Header("Refresh ", "3");

          Next, you have to find a way to pass the results from the asynchronous
          callback to the web page. I did this by passing the Session object in the
          IAsyncResult.As yncState field. The Session object can then be accessed
          from both the web page and the asynchronous callback function.

          Works nicely, but watch out for concurrency issues...

          Greetings,
          Wessel

          Comment

          • TS

            #6
            Re: Async method raised in web page doesn't refresh page

            I don't see how you can use the session in the async method, because that
            method is the start of the new thread that doesn't has access to HTTPContext
            object. Are you saying that you are passing the session object to the async
            method and NOT that you are using the session object in async method and set
            its return value to the session object?

            thanks for your posts.

            "Wessel Troost" <nothing@like.t he.sun> wrote in message
            news:op.sujfpza 5f3yrl7@asbel.. .[color=blue][color=green]
            > > Then, on the client side, you will have to poll some page over and
            > > over
            > > again until the operation completes. When it does, you can get the
            > > results
            > > and post them on the page accordingly.
            > >[/color]
            > The server side code can ask the client to refresh like this:
            >
            > // Ask client to refresh every 3 seconds
            > Response.Append Header("Refresh ", "3");
            >
            > Next, you have to find a way to pass the results from the asynchronous
            > callback to the web page. I did this by passing the Session object in the
            > IAsyncResult.As yncState field. The Session object can then be accessed
            > from both the web page and the asynchronous callback function.
            >
            > Works nicely, but watch out for concurrency issues...
            >
            > Greetings,
            > Wessel[/color]


            Comment

            • TS

              #7
              Re: Async method raised in web page doesn't refresh page

              thanks for all the feedback. There seems to be some potential here.


              "TS" <manofsteele1@n ospam.nospam> wrote in message
              news:OF%23uscfk FHA.2472@TK2MSF TNGP15.phx.gbl. ..[color=blue]
              > Im in a web page and call an asynchronous method in business class. the[/color]
              call[color=blue]
              > back method is in the web page.
              >
              > When page processes, it runs thru code begins invoking the method then the
              > page unloads. When the callback method is raised, only the method in the[/color]
              web[color=blue]
              > page is run and the page never refreshes, it seems it all happens on the
              > server side.
              >
              > I am trying to refresh the constrols on the page inside the callback[/color]
              method,[color=blue]
              > but when id do, it isn't flushed to the browser.
              >
              > Anyone know how i can do this with this long running async thread?
              >
              > thanks a bunch!
              >
              >[/color]


              Comment

              • Steven Cheng[MSFT]

                #8
                Re: Async method raised in web page doesn't refresh page

                Hi TS,

                AS for async processing in ASP.NET web application, direclty use the .net
                delegation's Beginxxx call won't work quite work since when the underlying
                working thread complete executing, the original httpRequest(in fact the
                whole Httpcontext) may already been disposed. So currently two possible
                async means we can choose are:
                1. make a central async processing httphandler which do the time consuming
                task. And we call this handler in the clientside page through clientside
                script (using MSXML.XMLHTTP component) and pull data from serverside so as
                to refresh our page.

                2. consider adjusting our page's design and using the asp.net's buildin
                page/handler level async processing model. The following msdn article(as
                also mentioend by Brock ) has demonstarte this mode:

                Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.


                Hope helps. Thanks,

                Steven Cheng
                Microsoft Online Support

                Get Secure! www.microsoft.com/security
                (This posting is provided "AS IS", with no warranties, and confers no
                rights.)

                --------------------
                | From: "TS" <manofsteele1@n ospam.nospam>
                | References: <OF#uscfkFHA.24 72@TK2MSFTNGP15 .phx.gbl>
                <OKnRyjfkFHA.35 68@tk2msftngp13 .phx.gbl> <op.sujfpza5f3y rl7@asbel>
                | Subject: Re: Async method raised in web page doesn't refresh page
                | Date: Tue, 26 Jul 2005 16:37:39 -0500
                | Lines: 32
                | X-Priority: 3
                | X-MSMail-Priority: Normal
                | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
                | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
                | Message-ID: <OnCEBpikFHA.33 16@TK2MSFTNGP14 .phx.gbl>
                | Newsgroups:
                microsoft.publi c.dotnet.framew ork.aspnet,micr osoft.public.do tnet.languages. c
                sharp
                | NNTP-Posting-Host: 103nat100.tea.s tate.tx.us 198.214.103.100
                | Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP14.phx. gbl
                | Xref: TK2MSFTNGXA01.p hx.gbl
                microsoft.publi c.dotnet.langua ges.csharp:1124 06
                microsoft.publi c.dotnet.framew ork.aspnet:1145 92
                | X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
                |
                | I don't see how you can use the session in the async method, because that
                | method is the start of the new thread that doesn't has access to
                HTTPContext
                | object. Are you saying that you are passing the session object to the
                async
                | method and NOT that you are using the session object in async method and
                set
                | its return value to the session object?
                |
                | thanks for your posts.
                |
                | "Wessel Troost" <nothing@like.t he.sun> wrote in message
                | news:op.sujfpza 5f3yrl7@asbel.. .
                | > > Then, on the client side, you will have to poll some page over and
                | > > over
                | > > again until the operation completes. When it does, you can get the
                | > > results
                | > > and post them on the page accordingly.
                | > >
                | > The server side code can ask the client to refresh like this:
                | >
                | > // Ask client to refresh every 3 seconds
                | > Response.Append Header("Refresh ", "3");
                | >
                | > Next, you have to find a way to pass the results from the asynchronous
                | > callback to the web page. I did this by passing the Session object in
                the
                | > IAsyncResult.As yncState field. The Session object can then be accessed
                | > from both the web page and the asynchronous callback function.
                | >
                | > Works nicely, but watch out for concurrency issues...
                | >
                | > Greetings,
                | > Wessel
                |
                |
                |

                Comment

                • Wessel Troost

                  #9
                  Re: Async method raised in web page doesn't refresh page

                  > I don't see how you can use the session in the async method, because that[color=blue]
                  > method is the start of the new thread that doesn't has access to
                  > HTTPContext object.[/color]

                  Accessing the HTTPContext from another thread might be OK, but I don't
                  think you can call it from an asynchronous callback. The context may not
                  be valid anymore when the callback is invoked.

                  But the Session is available to other threads for sure; the next thread
                  might be handled by another ASP.NET worker thread, after all.
                  [color=blue]
                  > Are you saying that you are passing the session object to the async
                  > method and NOT that you are using the session object in async methodand
                  > set its return value to the session object?
                  >[/color]
                  Actually I'm passing a reference to a data structure, like:

                  Context.Session[ "MyDataObje ct" ] = MyData;
                  AsyncCallback callback = new AsyncCallback( MyCallback );
                  BeginMyMethod( ..., callback, MyData );

                  The callback function looks like:

                  public static void MyCallback( IAsyncResult ar )
                  {
                  MyData = (MyDataClass) ar.AsyncState;
                  // Store results in MyData here.
                  // The Web Page can retrieve the results using
                  // Session["MyDataObje ct"].
                  }

                  Even if the Session ends, the MyData object will still exist.

                  Greetings,
                  Wessel

                  Comment

                  Working...