Creating Scheduled Tasks (.NET)

Posted on 2007-10-07
Last Modified: 2010-05-18
I'm looking for an efficent way to run dynamically created scheduled tasks e.g. I want a website user to define a date/time to run a job.

At the moment Im using windows services to repeatedly hit a db until the time matches - however this seem very in efficient - is there a better way to do this?

For the record I have access to SQL 2005 but this isn't necessary however Id like to develop this in 2005

Thanks in advance
Question by:paulCardiff
    LVL 22

    Expert Comment

    Some details might help.
    1. Can more than one user schedule a task (at different times or the same time) for a service?
    2. If you are currently writing information about the scheduled task to the DB, would you continue to do so if the service did not need to poll the DB?
    3. How time-critical are the tasks? For example, do they need to run to the neares ms, minute, hour, ect?

    LVL 11

    Expert Comment

    Windows services is the best solution.
    What I'd recomment is that you make an array (read from db once) holding the next 10 minutes of SHTasks
    When fineshed the 10 minutes, then you read the next Shceduled 10 minutes.

    Now in the mean time (inbetween the 10 minutes) some user might add a task to the db and the task was ment to run in the inbetween time frame.
    If this is not critical, then you will run the task from 1-10 minutes later.
    If it is critical, you may modify your website code to check the to alert your windows service, with something like a .txt file that you may read each time (within the 10 minutes) and see if something has to run again.

    Now if you want the exact time to match the Shedule, what you are doing now is the best. Hit the db every second

    Author Comment

    Hi Jim
    1) Yes more than one user can
    2) Yes i would hit the db
    3) Nearest Minute would be best way a explaining?

    Author Comment

    Hi AkisC:
    I agree this is an option but essentially i'm still hitting the db time and time again only at larger intervals -  thsi is something i've considered already but i'm curious if theres a better way e.g.

    Tapping into windows scheduler
    or using service broker

    But so far this is my best option - though i'll be honest not my favourite :-)
    LVL 11

    Accepted Solution

    Hi paulCardiff
    1) -windows scheduler=saves/loads jobs from a database... -service broker =the same

    2) ASP.NET was not designed to do any shceduled tasks, so the only you can do from a website is to insert-edit-delete a job you want to/from a database.
    2.1) Now you have to load the job in memory, as windows scheduler does.
    2.1.1) Shedule tasks are loaded into memory from a database created when you add a job.
    When you add a Shedule task from the Shedule tasks inteface, it can be loaded in memory at the same time because you use the SHTask inteface. --From there is not such an option

    3) Lot's of Windows services are running all the time in your computer, doing some kind of a job. They open a kind of database and check what must be done, when and so on..., so the best think you have to do is what you have already done.  A windowes service!  It runs in the background, separete tread and if your computer has ram, you should face no impact with performance.

    4) I'd recomment a 30 minute interval of getting the info from your database and strore jobs to be done in an array
    -Also- modify your .aspx code and when a new job is inserted then open a .txt file writting new job in
    -from- your windows service check on a timer 1 sec if the .txt file exists. If it exists then read info and ajust your array if a job is inbetwwen these 30 minutes otherwise ignore it and delete the file.
    That way you will not face any problem with performance.

    Windows services are my favourite ;-)
    Have fun codeing...

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now