all groups > dotnet web services > february 2008 >
You're in the

dotnet web services

group:

Problems starting a scheduler from Application_Start()


Problems starting a scheduler from Application_Start() austirob
2/25/2008 12:31:00 AM
dotnet web services:
Hi,

While I realise that this may not be a very nice solution architecturally, I
kick off a Scheduler in an ASP.NET 2.0 webservice in Application_Start()
which is supposed to call a stored proc in a database once a day every day.

The code works on the day that the webservice is deployed, but not on
subsequent days unless I restart IIS.

The code looks like this:

void Application_Start(object sender, EventArgs e)
{

Scheduler sch = new Scheduler();

}

public Scheduler()
{
Logger.Write("Initialising scheduler.... " + DateTime.Now,
"ABSPerformance");
System.Timers.Timer testTimer = new System.Timers.Timer();
testTimer.Enabled = true;
testTimer.Interval = 60 * 1000;

testTimer.Elapsed += new
System.Timers.ElapsedEventHandler(testTimer_Elapsed);

}



private void testTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
if (!DateTime.Now.DayOfWeek.Equals(DayOfWeek.Saturday) &&
!DateTime.Now.DayOfWeek.Equals(DayOfWeek.Sunday))
{

if (DateTime.Now.Hour.Equals(8) &&
DateTime.Now.Minute.Equals(30))
{
ABSData.executeNonQuery("StructuredCD.sp_ABS_Update",
null);
}

}

}

Is there a technical reason why I can't use the scheduler in this way, why
it only works on the first day?

As you have probably guessed, I am new to ASP.NET, so any help would be
appreciated.

Many thanks

Rob





Re: Problems starting a scheduler from Application_Start() John Saunders [MVP]
2/25/2008 7:36:24 AM
[quoted text, click to view]

You should probably let the database execute the stored proc once a day.
Alternatively, write a small console app to run it, and use the Windows Task
Scheduler to cause it to run periodically. This usage has nothing to do with
web services, which are not meant for anything like this!

I wouldn't be surprised to see your sch and testTimer objects being garbage
collected.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

Re: Problems starting a scheduler from Application_Start() austirob
2/25/2008 8:10:03 AM
Thanks John, agreed. I have moved the task to a SQL agent job. I guess I need
to read up on how garbage collection works in such circumstances. I 'm
surprised, as I would have thought that while the Scheduler object was
running it wouldn't get garbage collected. I will get it to write to a log
file once an hour and see if it makes any difference.

[quoted text, click to view]
Re: Problems starting a scheduler from Application_Start() John Saunders [MVP]
2/25/2008 12:27:02 PM
[quoted text, click to view]

Objects don't "run", as far as the garbage collector is concerned. It's a
question of whether there are references to the object. You were using a
local variable (method scoped) to refer to the scheduler, then another local
variable to refer to the timer object, and I wouldn't bet money that there's
no time after the timer goes off the first time, when there are still
references to all objects.

Anyway, you've made the right move. Nothing like that belongs in a web
service.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

AddThis Social Bookmark Button