Hi Martin,
From your description, you put a .net Timer component on the ASP.NET web
page and want to access this timer from a certain control on that page,
yes? As for this problem, here are some of my suggestions:
1. The timer is not a UI control so it is not added into control collection
and just be a member of the page class. So from a sub control on the page,
there isn't any buildin means to find the component. The only way is if we
know the web page's page class and explicitly access the timer through the
page's member. For example, we define a common base page class which has a
timer member field , then, we can access all those pages derived from the
base page as the base page class instance.
2. SINCE ASP.NET web page is request/response based, so the page's
lifecycle is only between the request comming to the server and the
respsonse be returned to client. Also, any components on the page also only
exist during the page's lifecycle. So I think use a timer at page level
won't work as what we expect because everytime a page is request, a new
timer is constructed and when the page is rendered out and unloaded , the
timer is also disposed. If you do want to do some repeatedly tasks
everytime a page is loaded on the serverside, or constantly post back a
page after a period of time, you can consider Cor 's suggestion about using
the client javascript "setTimeout" to constanly post back the page.
Also, here is a tech article discussing on building such a custom timer
server controL:
#Build a Custom AutoPostBack ASP.NET Page Timer ServerControl
http://www.eggheadcafe.com/articles/20021006.asp
In addition, in asp.net it's ok for us to build a Application scope
background timer which execute over the application's lifecycle and do some
background schedule tasks,here is also a blog thread discussing on this:
#Scheduled Execution in ASP.NET
http://weblogs.asp.net/ashben/archive/2003/10/11/31579.aspx
Also, I've also tried build a appliation level background and store it in
ApplicationState collection, that also works, for example:
protected void Application_Start(Object sender, EventArgs e)
{
System.Threading.Timer timer1 = new System.Threading.Timer(new
System.Threading.TimerCallback(this.CheckStatus),null,0,3000);
Application["timer"] = timer1;
}
Hope helps..
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)