473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 5786
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ron James | last post by:
I have a Form based dialog which kicks off a worker thread. The form has a progress bar, and a Cancel button. The Cancel button Aborts the thread, and when exiting, the thread attempts to Show a...
1
by: Joshua Russell | last post by:
Firstly my main method is like this: static void Main(string args) { // New Form Thread FormHandler myFormHandler = new FormHandler(); ThreadStart myThreadStart = new...
5
by: Claire | last post by:
My progress window is created by a secondary thread and then updated by it while a file is uploaded. There's an avi animation control on there that should show the move file avi. Plus a progress...
2
by: BG | last post by:
We're having trouble writing the code to update a UI control (label.Text) from a secondary thread. We're using C# with Windows Forms. We have a main form named MainForm, a splash screen form...
11
by: DW | last post by:
I've gotten this question a couple of times in interviews and I don't know what they are looking for: How do you update a control's property, such as a textbox.text property, from a thread, in...
0
by: Art Guerra | last post by:
Any tips or information would be most appreciated! Here is my scenario simplified: I have a main application form (Form1) and on that form is a listView control. On a separate form (Form2), the...
4
by: Roger | last post by:
I have a function that is currently wrapped up in a Class so I can pass a variable to it. This function is going to be threaded out and I would like the class function to be able to update a...
5
by: Mark R. Dawson | last post by:
Hi all, I may be missing something with how databinding works but I have bound a datasource to a control and everything is great, the control updates to reflect the state of my datasource when I...
0
by: Code Monkey | last post by:
Suppose I have a windows form (.exe) that has a load of labels and text boxes on it. I enter a number into one of the text boxes and hit the search button. This then launches another thread,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.