469,883 Members | 1,224 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,883 developers. It's quick & easy.

Updating Web Form Control From Thread

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!!
Nov 18 '05 #1
9 5272
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 2 Nov 2004 15:27:11 -0800, je**********@yahoo.com (Jervin Justin)
wrote:
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!!


Nov 18 '05 #2
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
}
}
Nov 18 '05 #3
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/de...nethowto08.asp

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 3 Nov 2004 07:08:39 -0800, je**********@yahoo.com (Jervin Justin)
wrote:
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
}
}


Nov 18 '05 #4
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

"Jervin Justin" <je**********@yahoo.com> wrote in message
news:cf**************************@posting.google.c om...
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
}
}

Nov 18 '05 #5
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!!
Nov 18 '05 #6
On 3 Nov 2004 15:48:31 -0800, je**********@yahoo.com (Jervin Justin)
wrote:
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/is.../CuttingEdge/), but a
web server can only "talk" to a client when the client initiates the
request.

Making any sense?

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 18 '05 #7
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
Nov 18 '05 #8
ck
javascript is very simple. Kind of similiar to C# in some areas
"Jervin Justin" <je**********@yahoo.com> wrote in message
news:cf*************************@posting.google.co m...
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

Nov 18 '05 #9

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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 8 Nov 2004 07:01:09 -0800, je**********@yahoo.com (Jervin Justin)
wrote:
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


Nov 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ron James | last post: by
1 post views Thread by Joshua Russell | last post: by
5 posts views Thread by Claire | last post: by
5 posts views Thread by Mark R. Dawson | last post: by
reply views Thread by Code Monkey | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.