?
Solved

How to implement a scheduled "Run At" date/time?

Posted on 2006-03-20
7
Medium Priority
?
244 Views
Last Modified: 2010-04-16
Hi all

I need to implement functionality in my Windows service such that it executes on a given day at a given time. The service has been written in C#.

I need to read a day + time string from a app.config file, e.g.

    <add key="run.at.day.time" value="Thursday, 18:00" />

though I'm open to persuasion about the format of this.

I can't seem to get this to work. The following attempt fails if the day of the week in the app.config file is NOT today(!).

    string[] myFormats = {"dddd, HH:mm"};

    DateTime runAt = DateTime.ParseExact(
            MyConfig.Read("run.at.day.time"),
            myFormats,
            new CultureInfo("en-GB"),
            DateTimeStyles.AllowWhiteSpaces
            );

And is there a way to persuade the DateTime instance that, if the timestamp in the config file is PRIOR to now, that it should assume the timestamp means NEXT WEEK?

Thanks.
0
Comment
Question by:seandynan
  • 3
  • 3
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16234519
if (runat.Subtract(DateTime.Now).TotalDays < 0)
   runat = runat.AddDays(7);

Bob
0
 

Author Comment

by:seandynan
ID: 16234572
Thanks Bob - that'll probably solve my closing question.

Have you any strategy for implementing my general scheduling task, how to generate a DateTime given just a day and time? At the moment my feeble attempt above is throwing a FormatException if the day isn't today.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16234873
Generally it is acceptable practice to only ask 1 question at a time, in order for these questions to double as a knowledge base, but I am feeling generous this morning.

Can I see your feeble attempt, please?

Bob

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:seandynan
ID: 16236288
> but I am feeling generous this morning.

LOL...okay, well my burning question really is, given a string containing a day and a time, how can I get my hands on a DateTime instance that reflects the next occurrence of that day & time?

For example, if today's date was Sunday, 01 Jan 2006 and I offered up "Friday, 13:00" in my config file, how would I generate a DateTime instance that reflected Friday, 06 Jan 2006, 13:00:00 ?


> Can I see your feeble attempt, please?

In a nutshell:

    string[] myFormats = {"dddd, HH:mm"};

    DateTime runAt = DateTime.ParseExact(
            "Friday, 13:00",
            myFormats,
            new CultureInfo("en-GB"),
            DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AdjustToUniversal
            );

This throws a FormatException ("String was not recognized as a valid DateTime") if the day of the week is NOT today.

Presumably the DateTime constructors expect an actual indication of some kind of date somewhere, and I'm not going to get away with the simple, single-step construction I'm attempting :-(

Thanks.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16236473
Try this method instead:

  public static DateTime TimestampToDate(string timeStamp)
  {

    int pos = timeStamp.IndexOf(",");
    if (pos == -1)
    {
      throw new ArgumentException("Invalid format for timestamp");
    }

    string dayWeek = timeStamp.Substring(0, pos);
    string time = timeStamp.Substring(pos + 1).Trim();
    DayOfWeek weekDay = ((DayOfWeek)(Enum.Parse(typeof(DayOfWeek), dayWeek)));

    pos = time.IndexOf(":");
    if (pos == -1)
    {
      throw new ArgumentException("Invalid time format");
    }

    int hour = Convert.ToInt32(time.Substring(0, pos));
    int minute = Convert.ToInt32(time.Substring(pos + 1));
    DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, 0);

    while (dt.DayOfWeek != weekDay)
    {
      dt = dt.AddDays(1);
    }

    return dt;

Bob
0
 

Author Comment

by:seandynan
ID: 16237321
Hey Bob, that works a treat. Many thanks.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

750 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