• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

Auto Run Procedures in Windows Console Application

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.
0
mlong219
Asked:
mlong219
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
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)
    {
        // WITHIN 5 MINUTES OF PREDAY
    }
}

Open in new window

0
 
mlong219Author Commented:
@kaufmed:

I get an error: error CS0019: Operator '-' cannot be applied to operands of type 'System.TimeSpan' and 'double'
0
 
käµfm³d 👽Commented:
Did you forget the parentheses (line 10)?
0
 
mlong219Author Commented:

@kaufmed:

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);
0
 
mlong219Author Commented:
This helped me meet my deadline!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now