Doesn't Timer class work in a Web Form?

  • Thread starter Thread starter Jim Hammond
  • Start date Start date
J

Jim Hammond

The following code tries to excute a function 10 seconds after Page_Load by using a Timer, but the callback never gets called.



private void Page_Load(object sender, System.EventArgs e)

{

// Set timer to call Page_PostLoad in 10 seconds

try

{

// Create the delegate that invokes methods for the timer.

TimerCallback timerDelegate = new TimerCallback(Page_PostLoad);

// Create a timer that invokes once after waiting 10 seconds.

Timer timer = new Timer( timerDelegate, this, 1000, 0 );

// Keep a handle to the timer, so it can be disposed.

tmr = timer;

}

catch(Exception ex)

{

ExceptionDisplay( ex );

}

}
 
Jim said:
The following code tries to excute a function 10 seconds after Page_Load
by using a Timer, but the callback never gets called.



private void Page_Load(object sender, System.EventArgs e)

{

// Set timer to call Page_PostLoad in 10 seconds

try

{

// Create the delegate that invokes methods for the timer.

TimerCallback timerDelegate = new TimerCallback(Page_PostLoad);

// Create a timer that invokes once after waiting 10 seconds.

Timer timer = new Timer( timerDelegate, this, 1000, 0 );

// Keep a handle to the timer, so it can be disposed.

tmr = timer;

}

catch(Exception ex)

{

ExceptionDisplay( ex );

}

}

Page objects have a very short lifetime (they get disposed when the
response is sent). You'll probably need to create the timer delegate
and store it in the application object context.

Note that by the time the timer fires, the page will be long gone, so
the delegate will not be able to do any useful work on that particular
page instance.
 
A page's lifetime on the server is measured in milliseconds. Just long enough to generate the HTML and send it out to the user.
You might want to consider a javascript client side timer since a page generally lives considerably longer on the user's machine.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net



The following code tries to excute a function 10 seconds after Page_Load by using a Timer, but the callback never gets called.



private void Page_Load(object sender, System.EventArgs e)

{

// Set timer to call Page_PostLoad in 10 seconds

try

{

// Create the delegate that invokes methods for the timer.

TimerCallback timerDelegate = new TimerCallback(Page_PostLoad);

// Create a timer that invokes once after waiting 10 seconds.

Timer timer = new Timer( timerDelegate, this, 1000, 0 );

// Keep a handle to the timer, so it can be disposed.

tmr = timer;

}

catch(Exception ex)

{

ExceptionDisplay( ex );

}

}
 
Page objects have a very short lifetime (they get disposed when the
response is sent). You'll probably need to create the timer delegate
and store it in the application object context.

Note that by the time the timer fires, the page will be long gone, so
the delegate will not be able to do any useful work on that particular
page instance.

--
mikeb

Thanks, but...

Although a page has a short lifespan, I have discovered that the timer
callback is in fact being called after 10 seconds.

The code below works except that calling Server.Transfer generates the
following exception, and I don't know why yet:

"Error executing child request for Form_Welcome.aspx."

Notet hat "p" appears to be perfectly valid and returns equal when compared
to the original Web Form object, which I saved using Application.Add
specifically to test for such equality.


private void Page_Load(object sender, System.EventArgs e)
{
// Set timer to call Page_PostLoad in 10 seconds
timerDelegate = new TimerCallback(Page_PostLoad);
timer = new Timer( timerDelegate, this, 10000, 0 );
}

static void Page_PostLoad(Object page)
{
Form_ProceedToDesk p = (Form_ProceedToDesk)page;
try
{
// stop timer
p.timer.Dispose();
p.timer = null;
p.GoHome();
}
catch(Exception ex)
{
p.ExceptionDisplay( ex );
}
}

public void GoHome( )
{
Server.Transfer("Form_Welcome.aspx");
}
 
Jim said:
Page objects have a very short lifetime (they get disposed when the
response is sent). You'll probably need to create the timer delegate
and store it in the application object context.

Note that by the time the timer fires, the page will be long gone, so
the delegate will not be able to do any useful work on that particular
page instance.

--
mikeb

Thanks, but...

Although a page has a short lifespan, I have discovered that the timer
callback is in fact being called after 10 seconds.

The code below works except that calling Server.Transfer generates the
following exception, and I don't know why yet:

"Error executing child request for Form_Welcome.aspx."

Notet hat "p" appears to be perfectly valid and returns equal when compared
to the original Web Form object, which I saved using Application.Add
specifically to test for such equality.


private void Page_Load(object sender, System.EventArgs e)
{
// Set timer to call Page_PostLoad in 10 seconds
timerDelegate = new TimerCallback(Page_PostLoad);
timer = new Timer( timerDelegate, this, 10000, 0 );
}

static void Page_PostLoad(Object page)
{
Form_ProceedToDesk p = (Form_ProceedToDesk)page;
try
{
// stop timer
p.timer.Dispose();
p.timer = null;
p.GoHome();
}
catch(Exception ex)
{
p.ExceptionDisplay( ex );
}
}

public void GoHome( )
{
Server.Transfer("Form_Welcome.aspx");
}

Ok, so the timer delegate keeps the page object from being garbage
collected. However, I'm guessing that the processing of the page
completes while the timer is waiting to fire, which puts the page into a
state that it doesn't allow much useful processing to occur.

For example, the Server.Transfer() method "terminates execution of the
current page and begins execution of a new page". The new page will
send its response to the client on the same HTTP connection. but if the
current page completes its processing, the HTTP connection will quite
possibly be closed, so where will the Server.Transfer() method send its
response?

Once again, I'm just guessing, but I think that even though the page
object might still be hanging around, it might not be in a mood to do
much work for you 10 seconds after it has sent its response.
 
Back
Top