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

C

charles

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
 
C

charles

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
 
B

Bruce Barker

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)
 
C

charles

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top