Async method raised in web page doesn't refresh page

T

TS

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!
 
M

Marina

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
W

Wessel Troost

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.AppendHeader("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.AsyncState 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
 
T

TS

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.
 
W

Wessel Troost

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.

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.
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?
Actually I'm passing a reference to a data structure, like:

Context.Session[ "MyDataObject" ] = 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["MyDataObject"].
}

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

Greetings,
Wessel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top