Updating Web Form Control From Thread

Discussion in 'ASP .Net' started by Jervin Justin, Nov 2, 2004.

  1. 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!!
     
    Jervin Justin, Nov 2, 2004
    #1
    1. Advertising

  2. Jervin Justin

    Scott Allen Guest

    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, (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!!
     
    Scott Allen, Nov 3, 2004
    #2
    1. Advertising

  3. 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
    }
    }
     
    Jervin Justin, Nov 3, 2004
    #3
  4. Jervin Justin

    Scott Allen Guest

    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

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

    On 3 Nov 2004 07:08:39 -0800, (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
    > }
    >}
     
    Scott Allen, Nov 3, 2004
    #4
  5. 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" <> wrote in message
    news:...
    > 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
    > }
    > }
     
    Kevin Spencer, Nov 3, 2004
    #5
  6. 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!!
     
    Jervin Justin, Nov 3, 2004
    #6
  7. Jervin Justin

    Scott Allen Guest

    On 3 Nov 2004 15:48:31 -0800, (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/issues/04/08/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/
     
    Scott Allen, Nov 4, 2004
    #7
  8. 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
     
    Jervin Justin, Nov 8, 2004
    #8
  9. Jervin Justin

    ck Guest

    javascript is very simple. Kind of similiar to C# in some areas
    "Jervin Justin" <> wrote in message
    news:...
    > 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
     
    ck, Nov 8, 2004
    #9
  10. Jervin Justin

    Scott Allen Guest

    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, (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
     
    Scott Allen, Nov 8, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FET
    Replies:
    2
    Views:
    2,647
    Filip Larsen
    Jan 8, 2005
  2. mkrause
    Replies:
    0
    Views:
    675
    mkrause
    May 6, 2005
  3. Replies:
    7
    Views:
    299
    Tim Mackey
    Jan 18, 2007
  4. Jason Cavett

    Updating GUI Components From A Thread

    Jason Cavett, Apr 4, 2007, in forum: Java
    Replies:
    5
    Views:
    603
    Brandon McCombs
    Apr 7, 2007
  5. news.rcn.com
    Replies:
    2
    Views:
    1,127
    Roedy Green
    Dec 10, 2007
Loading...

Share This Page