Timer Execution

  • Thread starter Thread starter Yama
  • Start date Start date
Y

Yama

Hi,

Is there a way to execute a function after 1 minute on server-side once a
web form is rendered? Can you please show me some code to illustrate this?

Thanks,

~yamazed
 
In your page class, create a method that calls
Thread.Sleep(TimeSpan.FromMinutes(1)) and then executes some code. Create a
new thread to call that method. It will cause the thread in which the method
is running to sleep, not the main thread.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
BTW, I should mention that using a timer would be a waste. Timers are only
useful if you want to repeat something at intervals.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
Kevin,

Let's say I have my page default.aspx that has render to client browser what
you are telling me is that I could use the Thread.Sleep static method. But
what event shall I tie a delegate to to fire this method?

Can you drop a quick code illustration?

Thanks,

~yamazed
 
As you want to run it as soon before the page is sent to the client, create
an event handler for the PreRender event, and call your method that executes
the code from that event handler. Example:

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.PreRender +=new EventHandler(Page1_PreRender);
}

private void Page1_PreRender(object sender, EventArgs e)
{
Thread myThread = new Thread(new ThreadStart(this.WaitAMinute));
myThread.Start();
}

private void WaitAMinute()
{
Thread.Sleep(TimeSpan.FromMinutes(1));
// Do Something
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
Back
Top