Auto Run Procedures in Windows Console Application

Posted on 2011-05-04
Last Modified: 2012-06-27
I have a weird problem. I was asked to modify a console application that is using the windows scheduler, and runs every 10min around the clock. I can't change it to a service because the change needs to be made quickly, but I need to be able to do a preday check at 11:55 midnight, 5:50 AM, and 5:50 PM.

Its very difficult to keep track of the time because the program launches every 10min. Is there a way I can check to see if I'm within 5-10min of the preday check dates using c#? I was thinking I could then save the last time the preday check was last ran into the database, then pull the lastrun date later when the program kicks off again, to see if it already had ran for the day. Not sure which date/time functions I need to use.

Any ideas would be appreciated.
Question by:mlong219
    LVL 74

    Accepted Solution

    I can't change it to a service because the change needs to be made quickly,
    It's actually very simple to convert between the two.

    Is there a way I can check to see if I'm within 5-10min of the preday check dates using c#?
    Try the following:
    TimeSpan[] predayTimes = new TimeSpan[3]
        new TimeSpan(5, 50, 0),
        new TimeSpan(17, 50, 0),
        new TimeSpan(23, 55, 0)
    foreach (TimeSpan preday in predayTimes)
        int minutes = (preday - DateTime.Now.TimeOfDay).TotalMinutes;
        if (minutes >= 0 && minutes <= 5)

    Open in new window


    Author Comment


    I get an error: error CS0019: Operator '-' cannot be applied to operands of type 'System.TimeSpan' and 'double'
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Did you forget the parentheses (line 10)?

    Author Comment



    You mean remove the parentheses?
    I tried the following and it appears to work, but noticed some of the minutes have a minus sign in front of the value...

    int minutes = Convert.ToInt32((preday - DateTime.Now.TimeOfDay).TotalMinutes);

    Author Closing Comment

    This helped me meet my deadline!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    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…

    779 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

    9 Experts available now in Live!

    Get 1:1 Help Now