Im trying to create a timer in my Application base to do some background tasks. For some reason, my timer just doesnt seem to want to fire its event though. This is my stripped down sample code:
public class Global : System.Web.HttpApplication
{
private static System.Timers.Timer timer;
public Global()
{
timer = new System.Timers.Timer(5000);
((System.ComponentModel.ISupportInitialize)(timer)).BeginInit();
timer.Elapsed += new System.Timers.ElapsedEventHandler(testfire);
timer.Start();
((System.ComponentModel.ISupportInitialize)(timer)).EndInit();
}
public void testfire(Object sender, System.Timers.ElapsedEventArgs e)
{
System.IO.StreamWriter x = System.IO.File.AppendText(Server.MapPath("/log.txt"));
x.WriteLine("testfire at " + System.DateTime.Now);
x.Close();
}
protected void Application_Start(Object sender, EventArgs e)
{
System.IO.StreamWriter x = System.IO.File.AppendText(Server.MapPath("/log.txt"));
x.WriteLine("Application starting at " + System.DateTime.Now);
x.Close();
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
System.IO.StreamWriter x = System.IO.File.AppendText(Server.MapPath("/log.txt"));
x.WriteLine("Request beginning at " + System.DateTime.Now);
x.Close();
}
protected void Application_EndRequest(Object sender, EventArgs e)
{
System.IO.StreamWriter x = System.IO.File.AppendText(Server.MapPath("/log.txt"));
x.WriteLine("Request ending at " + System.DateTime.Now);
x.Close();
}
protected void Application_End(Object sender, EventArgs e)
{
System.IO.StreamWriter x = System.IO.File.AppendText(Server.MapPath("/log.txt"));
x.WriteLine("Application ending at " + System.DateTime.Now);
x.Close();
}
}
This is on my personal machine that Im testing this on.
I call testfire manually, that works. I check to see that the timer is being created, it is. I check to see if its interval is correct and that it is enabled, it is... The event just wont fire. I get a line in my log for application starting, requests beginning
and requests ending. Thats it. My application_end event does seem to want to fire either. I tried this same code on my web host, and it seems to work great there. I use visual studio, and application_end fires on the web host when I build. But not on my local
machine. I dont get it, what could cause this? Thanks
Alright, so with a little help from the vs.net debugger, I was able to figure out what was going on. It doesnt like that I am calling the server operation Server.MapPath from the context of my timer event or application_end event, and throws an exception. So
the events were being fired after all, just not making it all the way through. I am still not really very sure what this means exactly though. It was just fine calling Server.MapPath from the other events, so why is it unhappy in only these events? And how
can I avoid potentially making the same mistake in the future of calling a method that I shouldnt be calling in a particular context. Was I just supposed to know that I wasnt allowed to make that call there? Also, can someone gimme a clue as to where System.Diagnostics.Debug.WriteLine
writes to? Thanks!
dennispg
Member
37 Points
9 Posts
timer events and application_end event wont fire
Nov 26, 2003 06:53 AM|LINK
public class Global : System.Web.HttpApplication { private static System.Timers.Timer timer; public Global() { timer = new System.Timers.Timer(5000); ((System.ComponentModel.ISupportInitialize)(timer)).BeginInit(); timer.Elapsed += new System.Timers.ElapsedEventHandler(testfire); timer.Start(); ((System.ComponentModel.ISupportInitialize)(timer)).EndInit(); } public void testfire(Object sender, System.Timers.ElapsedEventArgs e) { System.IO.StreamWriter x = System.IO.File.AppendText(Server.MapPath("/log.txt")); x.WriteLine("testfire at " + System.DateTime.Now); x.Close(); } protected void Application_Start(Object sender, EventArgs e) { System.IO.StreamWriter x = System.IO.File.AppendText(Server.MapPath("/log.txt")); x.WriteLine("Application starting at " + System.DateTime.Now); x.Close(); } protected void Application_BeginRequest(Object sender, EventArgs e) { System.IO.StreamWriter x = System.IO.File.AppendText(Server.MapPath("/log.txt")); x.WriteLine("Request beginning at " + System.DateTime.Now); x.Close(); } protected void Application_EndRequest(Object sender, EventArgs e) { System.IO.StreamWriter x = System.IO.File.AppendText(Server.MapPath("/log.txt")); x.WriteLine("Request ending at " + System.DateTime.Now); x.Close(); } protected void Application_End(Object sender, EventArgs e) { System.IO.StreamWriter x = System.IO.File.AppendText(Server.MapPath("/log.txt")); x.WriteLine("Application ending at " + System.DateTime.Now); x.Close(); } }This is on my personal machine that Im testing this on. I call testfire manually, that works. I check to see that the timer is being created, it is. I check to see if its interval is correct and that it is enabled, it is... The event just wont fire. I get a line in my log for application starting, requests beginning and requests ending. Thats it. My application_end event does seem to want to fire either. I tried this same code on my web host, and it seems to work great there. I use visual studio, and application_end fires on the web host when I build. But not on my local machine. I dont get it, what could cause this? Thanksdennispg
Member
37 Points
9 Posts
Re: timer events and application_end event wont fire
Nov 26, 2003 04:45 PM|LINK