Doesn't Timer class work in a Web Form?

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

}

}
 
M

mikeb

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.
 
S

Steve C. Orr [MVP, MCSD]

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

}

}
 
J

Jim Hammond

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");
}
 
M

mikeb

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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top