Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / February 2008

Tip: Looking for answers? Try searching our database.

Problems starting a scheduler from Application_Start()

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
austirob - 25 Feb 2008 08:31 GMT
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
John Saunders [MVP] - 25 Feb 2008 12:36 GMT
> Hi,
>
[quoted text clipped - 3 lines]
> which is supposed to call a stored proc in a database once a day every
> day.

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.
Signature

--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

austirob - 25 Feb 2008 16:10 GMT
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.  

> > Hi,
> >
[quoted text clipped - 11 lines]
> I wouldn't be surprised to see your sch and testTimer objects being garbage
> collected.
John Saunders [MVP] - 25 Feb 2008 17:27 GMT
> 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.

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.
Signature

--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.