473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Doesn't Timer class work in a Web Form?

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
8 1817
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Anthony Boudouvas | last post by:
Hi to all, i have a form with 2 System.Windows.Forms.Timer objects. One fire every 5 seconds and the other every 10 seconds, the both take actions in two hashtables declared in same form. ...
4
by: Bilo | last post by:
I have a Windows Forms Class MainGUI I have declared MainGUI maingui; public System.ComponentModel.Container components = new Container(); in the Class I call another class MediaDriver with...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
10
by: WhiteSocksGuy | last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a System.Timers.Timer to work for me to display a splash screen for about two seconds and then load the main form. I have...
5
by: muzilli | last post by:
Howdy all, I would like to know how can I insert a Timer object in my class library? This timer object will start and stop in a determinated part or event of my program. I know how to do...
11
by: Hotrod2000 | last post by:
I'm quite new to programming but I'm having problems getting a timer to work in visual studio.net I've created a timer on a form, enabled it and then typed the following code (from the mdsn...
16
by: Peter Oliphant | last post by:
Note that although this involves SAPI, it is more a question about Timers and event handlers. I wrote a Speech Recognize handler (SAPI), and put some code in it to enable a Timer. It would not...
1
by: truedecembr | last post by:
Hi everyone, I am brand new to Java and not really even sure what I'm doing... I'm supposed to be writing a Timer class that is part of a stop watch application, and it seems to me that the program...
11
by: winningElevent | last post by:
This code C# is from online source but it was in console application so I modify to work on windows form just for learning on how to use threading.timer, but it doesn't work. Can anyone help me? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.