473,654 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5816
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**********@ya hoo.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.waitForRepl y (); //The service automatically sends
updates from this method
textBoxUpdate.T ext = 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**********@ya hoo.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.waitForRepl y (); //The service automatically sends
updates from this method
textBoxUpdate.T ext = 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**********@y ahoo.com> wrote in message
news:cf******** *************** ***@posting.goo gle.com...
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.waitForRepl y (); //The service automatically sends
updates from this method
textBoxUpdate.T ext = 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,howeve r, 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**********@ya hoo.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**********@y ahoo.com> wrote in message
news:cf******** *************** **@posting.goog le.com...
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**********@ya hoo.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
6697
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 hidden control on the form. This does not work, and I'm pretty sure it's because the Show method is being called from a different thread. Closer inspection of the documentation for Form and the various controls confirms this. (Static members...
1
1401
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 ThreadStart(myFormHandler.ShowForm); Thread formWorkerThread = new Thread(myThreadStart); formWorkerThread.Start();
5
2187
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 bar. Im having problems as the screen isn't being redrawn properly. If I call DoEvents each time then it works ok. I want to dump DoEvents to prevent problems. I'm calling the following from the progress property set function. InvokeRequired...
2
11780
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 named SplashScreen, and a C# class library named BackgroundProcess.
11
2220
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 .NET? It seems to me you just update it normally. What are the interviewers looking for here? Thanks. - W
0
1391
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 user is able to select a folder using a folderBrowser control. I want the paths to all files to be added to the listView control on Form1. My question lies around what thread should perform the item additions. Currently, on Form2, I referenced...
4
1203
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 control on my form. (Treeview). Is this possible and how do I do it? Here is a simplisitc overview of what I am doing...
5
12514
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 update the datasource - awesome, but I have an issue with updating from a different thread. Here is my datasource, a person class that raises the PropertyChanged event: class Person : INotifyPropertyChanged {
0
1587
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, which may or may not spawn other threads. What's the best way of updating the user interface?
0
8814
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8706
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7304
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.