By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,342 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,342 IT Pros & Developers. It's quick & easy.

Doesn't Timer class work in a Web Form?

P: n/a
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 );

}

}

Nov 17 '05 #1
Share this Question
Share on Google+
8 Replies

P: n/a
Jim Hammond wrote:
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

Nov 17 '05 #2

P: n/a
Jim Hammond wrote:
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

Nov 17 '05 #3

P: n/a
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

"Jim Hammond" <jh******@postalinnovations.com> wrote in message news:um**************@tk2msftngp13.phx.gbl...
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 );

}

}

Nov 17 '05 #4

P: n/a
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

"Jim Hammond" <jh******@postalinnovations.com> wrote in message news:um**************@tk2msftngp13.phx.gbl...
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 );

}

}

Nov 17 '05 #5

P: n/a
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");
}
Nov 17 '05 #6

P: n/a
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");
}
Nov 17 '05 #7

P: n/a
Jim Hammond wrote:
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.

--
mikeb

Nov 17 '05 #8

P: n/a
Jim Hammond wrote:
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.

--
mikeb

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.