473,782 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(objec t sender, System.EventArg s e)

{

// Set timer to call Page_PostLoad in 10 seconds

try

{

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

TimerCallback timerDelegate = new TimerCallback(P age_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)

{

ExceptionDispla y( ex );

}

}

Nov 17 '05 #1
8 1838
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(objec t sender, System.EventArg s e)

{

// Set timer to call Page_PostLoad in 10 seconds

try

{

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

TimerCallback timerDelegate = new TimerCallback(P age_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)

{

ExceptionDispla y( 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(objec t sender, System.EventArg s e)

{

// Set timer to call Page_PostLoad in 10 seconds

try

{

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

TimerCallback timerDelegate = new TimerCallback(P age_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)

{

ExceptionDispla y( 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******@posta linnovations.co m> wrote in message news:um******** ******@tk2msftn gp13.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(objec t sender, System.EventArg s e)

{

// Set timer to call Page_PostLoad in 10 seconds

try

{

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

TimerCallback timerDelegate = new TimerCallback(P age_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)

{

ExceptionDispla y( 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******@posta linnovations.co m> wrote in message news:um******** ******@tk2msftn gp13.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(objec t sender, System.EventArg s e)

{

// Set timer to call Page_PostLoad in 10 seconds

try

{

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

TimerCallback timerDelegate = new TimerCallback(P age_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)

{

ExceptionDispla y( 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.as px."

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(objec t sender, System.EventArg s e)
{
// Set timer to call Page_PostLoad in 10 seconds
timerDelegate = new TimerCallback(P age_PostLoad);
timer = new Timer( timerDelegate, this, 10000, 0 );
}

static void Page_PostLoad(O bject page)
{
Form_ProceedToD esk p = (Form_ProceedTo Desk)page;
try
{
// stop timer
p.timer.Dispose ();
p.timer = null;
p.GoHome();
}
catch(Exception ex)
{
p.ExceptionDisp lay( 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.as px."

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(objec t sender, System.EventArg s e)
{
// Set timer to call Page_PostLoad in 10 seconds
timerDelegate = new TimerCallback(P age_PostLoad);
timer = new Timer( timerDelegate, this, 10000, 0 );
}

static void Page_PostLoad(O bject page)
{
Form_ProceedToD esk p = (Form_ProceedTo Desk)page;
try
{
// stop timer
p.timer.Dispose ();
p.timer = null;
p.GoHome();
}
catch(Exception ex)
{
p.ExceptionDisp lay( 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.as px."

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(objec t sender, System.EventArg s e)
{
// Set timer to call Page_PostLoad in 10 seconds
timerDelegate = new TimerCallback(P age_PostLoad);
timer = new Timer( timerDelegate, this, 10000, 0 );
}

static void Page_PostLoad(O bject page)
{
Form_ProceedToD esk p = (Form_ProceedTo Desk)page;
try
{
// stop timer
p.timer.Dispose ();
p.timer = null;
p.GoHome();
}
catch(Exception ex)
{
p.ExceptionDisp lay( 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.as px."

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(objec t sender, System.EventArg s e)
{
// Set timer to call Page_PostLoad in 10 seconds
timerDelegate = new TimerCallback(P age_PostLoad);
timer = new Timer( timerDelegate, this, 10000, 0 );
}

static void Page_PostLoad(O bject page)
{
Form_ProceedToD esk p = (Form_ProceedTo Desk)page;
try
{
// stop timer
p.timer.Dispose ();
p.timer = null;
p.GoHome();
}
catch(Exception ex)
{
p.ExceptionDisp lay( 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
4030
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. When timers fire, main form is somewhat blocking until timers finish their job, (socket operations). (Imagine to move the form by it's caption bar and it somewhat freeze when timers fire...)
4
464
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 the Constructor class MediaDriver {
6
2883
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 TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things, periodically calls a method in this TimerState object which raises an event to the startup form,...
10
2708
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 two forms (frmSplash and frmMain) and a code Module setup as my startup object. Here is the code that I have, when I try to run this part of the splash screen form shows and then program terminates. What am I doing wrong? Thanks,
5
4916
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 this in Delphi or using a RAD tool and insert the Timer object in a form, but how to do in C# by hand and in a class library (without form), I don't know.
11
2605
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 library as I thought this would be a good start!!!) but nothing happens :- Imports System.Timers
16
3257
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 do it. If I bring this same code outside this event handler, it works just fine. Is this normal?
1
1623
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 is correct, but when I run the tester, it is obviously not. The goal is to enter a base and a time, and find time % base, then tell how many times it cycled back to zero. I don't know what is wrong with my program, because it appears to me that it...
11
1485
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? I would like the code below to display message to richtextbox while the timer object is not yet destroy so the main thread shouldn't run. When timer object is destroy, the main thread will start and display the last message, "Timer example done." ...
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9942
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.