Why calling Button_Click() behaves differently from really clicking the button?

Discussion in 'ASP .Net' started by charles, Mar 11, 2006.

  1. charles

    charles Guest

    Hi:

    In my page I need to re-start a thread. I put the code into a
    Button_Click(sender, e) handler. When I click the button, it works.
    Then I call this handler inside a timer's handler timer1_Elapsed(object
    sender, System.Timers.ElapsedEventArgs e). I can trace the code and see
    it really go through the route. But it does not work. I added
    Page_Load(sender, e) after the Button_Click. It does not help.

    Can somebody give me a clue? Thanks!

    Charles
    3/11
     
    charles, Mar 11, 2006
    #1
    1. Advertising

  2. charles

    charles Guest

    I discovered that timer1_Elapsed() is in a different thread than the
    main page thread, while Button_Click() is in the main thread. Maybe
    that is the reason.

    I don't know how to handle the thread to automate Button_Click() so
    that no human intervention is needed. Please help.

    Charles
    3/13
     
    charles, Mar 13, 2006
    #2
    1. Advertising

  3. charles

    Bruce Barker Guest

    its the same thread, but the request has completed (rendered html sent back
    to browser). in fact by the time the timer fires, the thread may be
    processing the next request. you need to put a wait in your page (say in
    prerender) for your background thread to compelete, if it needs to update
    the rendered html.


    -- bruce (sqlwork.com)






    "charles" <> wrote in message
    news:...
    >I discovered that timer1_Elapsed() is in a different thread than the
    > main page thread, while Button_Click() is in the main thread. Maybe
    > that is the reason.
    >
    > I don't know how to handle the thread to automate Button_Click() so
    > that no human intervention is needed. Please help.
    >
    > Charles
    > 3/13
    >
     
    Bruce Barker, Mar 13, 2006
    #3
  4. charles

    charles Guest

    Dear Bruce:

    Please teach me how to write in prerender in order to wait for the
    background thread to complete.

    I don't quite understand your answer. I used a statement aThread =
    System.Threading.Thread.CurrentThread and put it in Page_Load() and
    timer1_Elapsed() to identify if they are the same thread (by looking at
    "DONT_USE_InternalThread" value, which is a unique int for each thread.
    Is this the right way?). I found that their values are different.

    What is the "background thread" you referred to?

    Below is the simple code I used for testing: (The text never showed
    up.)

    ==========================================================
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Timers.Timer timer1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    }
    #region Web Form Designer generated code
    ........................
    #endregion

    private void timer1_Elapsed(object sender,
    System.Timers.ElapsedEventArgs e)
    {
    TextBox1.Text = "Here!";
    }
    }
    ===========================================================

    Thanks a lot!

    Charles
    3/15
     
    charles, Mar 15, 2006
    #4
    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. Do
    Replies:
    1
    Views:
    345
    Natty Gur
    Nov 16, 2003
  2. Simon Kennedy
    Replies:
    0
    Views:
    345
    Simon Kennedy
    Jan 30, 2004
  3. Cindy Huyser
    Replies:
    1
    Views:
    526
    Cindy Huyser
    Aug 7, 2003
  4. otsarri
    Replies:
    3
    Views:
    397
    otsarri
    Sep 11, 2004
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,072
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page