Updating Web Form Control From Thread

J

Jervin Justin

Hi,

I've been having this problem for some time with Web Forms:

I have a web app that sends data to a service when the user presses a
button. Based on the data, the server will send several replies. Since
there is no way for a service to post to a client, I am using a thread
on the client that polls the server for updates.
(Feel free to recommend changes to that as well)

Now heres the problem... If I try to update a control from this thread
(to reflect the update received), obviously no change occurs on the
page cos its on a separate thread and doesn't have access to the
controls. I know in windows form, you can use MethodInvoker from a
thread to update controls... Any way to do this in Web Forms?

Thanks for any help!!
 
S

Scott Allen

Hi Jervin:

WebControls do not have the same thread affinity that WinForm controls
do, and so they do not provide the InvokeRequired / Invoke methods
that the WinForm controls have.

I'm getting a little lost in your question, sorry. Perhaps you could
post some code? I'm not sure when you say "client" if you mean the
browser or your web application. It sounds as if the webform has
already rendered to the client when you are modifying the controls,
which would be too late for the changes to appear.
 
J

Jervin Justin

Well let me try to generalize.

I have a web application (web Form)running on a server (ServerA),
which connects to a web service (running either on the same server or
different, no matter). When a user opens the web app, and clicks a
button, the app sends info to the service. The service now has
multiple updates which it has to send back to the web application

Is there anyway to update the text (not recreate a new control)in one
of my textboxes on the webform based on information that comes to the
web application as soon as it gets it?

Remember since I am polling, I get the information on a separate
thread. If there is any better way, I am oopen so suggestions.

Code snippet follows

thanks again.
Jervin

//Part of Web Application (WebForm)
//In Page_Load
if (!isPostBack)
{
svc = new webService ();
Thread poll = new Thread (new ThreadStart (update));
poll.Start();
}

//Thread:
public void update ()
{
while (true)
{
string str = svc.waitForReply (); //The service automatically sends
updates from this method
textBoxUpdate.Text = str; //Does not update since in separate
thread
}
}
 
S

Scott Allen

Hi Jervin:

The problem with using a seperate thread is that you still need to
block the response until that second thread returns data. From the
code here it looks as if the second thread starts and then the page
processing continues as normal, meaning Page_Load finishes, the
controls will render, and the HTML is sent to the client. Once all
that happens it is too late to make any changes to the controls, the
response has completed and nothing else will be sent to the browser.

My suggestion is to try without using a second thread - it won't buy
anything in terms of of responsiveness.

This article also has some ideas if you want to send a response to the
browser client immediately and then do the web service work while the
client (browser) polls:

How To: Submit and Poll for Long-Running Tasks
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenethowto08.asp
 
K

Kevin Spencer

Threading in ASP.Net is tricky, as the Page class and its components live
for such a short period of time (usually milliseconds). Chances are, by the
time your thread wants to update the textbox, it is no longer there. Of
course, you could have the Page class launch the thread and then wait for
it, but what would the use of a separate thread be in that case?

BTW, your thread code contains an infinite loop.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
J

Jervin Justin

Yeah, I see how my second thread is pointless cos once the page loads,
its controls aren't accessible anymore. Oh and I wasn't thinking about
responsiveness wehn I added the tread.

Kevin: Yeah, i know about the infinite loop, its so that once the
thread gets one response, it will call the method again automatically
and wait for a new response.

Thanks for the link Scott, it explains some stuff...
From what I understand from the tutorial,however, it keeps refreshing
the page every few seconds just to check if there is an update. This
is the problem, we do not want to refresh the page until we know there
is somethign to update.

One way I can think of is to have the second thread (instead of
updating the textBox) refresh the page when waitForReply returns. This
way it would refresh only when the webservice returns an update. Is it
possible to do this?

Or let me explain whats going on in my program a little further. Its
sort of like a chat client. If I open a web form, it connects to a web
service. Now, if another client (on another computer) also opens the
form and connects to the service, and then sends a message, I need to
be able to update information on my web page based on this. But I
don't want to have to reload the page every couple of seconds (becuase
in our real app, we can go hours without getting updates), but only
want to reload/refresh/update (either way) when there is somethign to
update.
Any suggestions on how to go about doing this? (I can do it with
windows forms, but I need it in a webpage to avoid forcing the clients
to use an exe, so they can just run it off thier browser).

Thanks!!
 
S

Scott Allen

One way I can think of is to have the second thread (instead of
updating the textBox) refresh the page when waitForReply returns. This
way it would refresh only when the webservice returns an update. Is it
possible to do this?


Jervin:

This is what you'll have to come to grips with conceptually:

The thread calling the web service is executing on the server. This
thread is the only thing that knows when the web service is ready with
results.

The web browser is executing on the client's computer. There is no way
for the server thread to reach out to the browser on another computer
and tell it something interesting has happened. The browser has to go
to the server and ask if anything interesting has happened.

This is what makes web applications "fun".

It's impossible to do what you want without the client going back to
the server. This isn't necessarily a full refresh (see
http://msdn.microsoft.com/msdnmag/issues/04/08/CuttingEdge/), but a
web server can only "talk" to a client when the client initiates the
request.

Making any sense?
 
J

Jervin Justin

Thanks for the link, it was really helpful. I understand the situation
a lot better now, and even have it doing what I want using javascript.

I have one question though, does client side code have to be in
javascript? Or can I write it in C#?

If it does have to be in C#, I was wondering if it were possible for
my client side javascript code to call a server side C# function or
use on of its variables.

I guess my next task is to get familiarlized w/ javascript.

Thanks again,
Jervin Justin
 
S

Scott Allen

HI jervin:

Client side has to use either JavaScript or VBScript (if you target IE
only). It is possible to have .NET code running on the client, this is
similar to running ActiveX controls on the client so it may not be
what you are looking for.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top