0

I want to copy files in 15-minute intervals. I do not want to use Task Scheduler.

Essentially, I execute all three threads and let the threads take care of themselves. Method RunWhenItsTime() basically monitors the current time and checks that RunTime is equal to CurrentTime. If it is, then RunWhenItsTime() calls CopyFiles() which begins copying files.

Should the following code work? For some reason, it gets stuck somewhere but I'm not sure where it is.

Here's my code:

Class myClass0 = new Class();
Thread0 = new Thread(() => myClass0.RunWhenItsTime(FirstQtrRun, 0, cts0.Token));
Thread0.Start();

Class myClass1 = new Class();
Thread1 = new Thread(() => myClass1.RunWhenItsTime(SecondQtrRun, 1, cts1.Token));
Thread1.Start();

Class myClass2 = new Class();
Thread2 = new Thread(() => myClass2.RunWhenItsTime(ThirdQtrRun, 2, cts2.Token));
Thread2.Start();

Class myClass3 = new Class();
Thread3 = new Thread(() => myClass3.RunWhenItsTime(FourthQtrRun, 3, cts2.Token));
Thread3.Start();

public void RunWhenItsTime(DateTime RunTime, int Period, Object obj)
{
    Console.WriteLine("Task Has been set to run at: " + RunTime.ToString(), " for Period: " + Period.ToString());
    DateTime CurrentTime = DateTime.Now;
    for (; ; )
    { //Check that CurrentTime is equal to RunTime.
        Console.WriteLine("Runtime: " + RunTime.ToString() + "   |   " + "Current Time: " + CurrentTime.ToString() 
            + "PERIOD: " + Period.ToString());
        if ((CurrentTime.Year == RunTime.Year) && (CurrentTime.Month == RunTime.Month) &&
            (CurrentTime.Day == RunTime.Day) && (CurrentTime.Hour == RunTime.Hour) &&
            (CurrentTime.Minute == RunTime.Minute))
        {
            CopyFiles(RunTime, Period, obj);
            break;
        }
        CurrentTime = DateTime.Now;
    }
}
4
  • 2
    Are you seriously using infinite loop instead of a timer or - even better - something like Quartz? May 12, 2014 at 13:28
  • 1
    Maybe they're using CPU utilization to check whether the scheduler still runs or not ;-)
    – Joey
    May 12, 2014 at 13:29
  • I would prefer using Timers but haven't found a decent example. Let's say I want to invoke CopyFiles() at :00, :15, :30, :45. How would I do this with a Threading.Timer? May 12, 2014 at 13:57
  • @PatrykĆwiek, I would prefer using Timers but haven't found a decent example. Let's say I want to invoke CopyFiles() at :00, :15, :30, :45. How would I do this with a Threading.Timer? May 12, 2014 at 14:03

2 Answers 2

2

First off your executing an infinite loop without letting the thread sleep when there is nothing to do (and your doing that 3 times), this will kill your CPU utilization:

for (; ; )
{ //Check that CurrentTime is equal to RunTime.
    Console.WriteLine("Runtime: " + RunTime.ToString() + "   |   " + "Current Time: " + CurrentTime.ToString() 
        + "PERIOD: " + Period.ToString());
    if ((CurrentTime.Year == RunTime.Year) && (CurrentTime.Month == RunTime.Month) &&
        (CurrentTime.Day == RunTime.Day) && (CurrentTime.Hour == RunTime.Hour) &&
        (CurrentTime.Minute == RunTime.Minute))
    {
        CopyFiles(RunTime, Period, obj);
        break;
    }
    CurrentTime = DateTime.Now;
    System.Threading.Thread.Current.Sleep(1000);  // sleep 1 second
}

Secondly, you really should only have 1 loop, this single loop should determine if it's time to CopyFiles for any one of your 3 conditions and execute (Thread/Task it out if you want) that. Once all 3 conditions are met, exit your timing loop.

1
  • Thanks. I had previously tried using a single loop to execute the threads, but there seemed to be an issue with the program being invoked. But now it works well. Thanks for the post. May 13, 2014 at 20:43
1

You probably shouldn't be using an infinite loop to do your direct testing of current time with time to kick of file copying. A better solution would be to use a C#'s Timer class. The following link has a very good example.

Using C# Timer class

Using this method you could still use your threads but not eat up all of your CPU and would likely be MUCH more reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.