Reputation: 756
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;
}
}
Upvotes: 0
Views: 137
Reputation: 6018
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 this method you could still use your threads but not eat up all of your CPU and would likely be MUCH more reliable.
Upvotes: 1
Reputation: 12847
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.
Upvotes: 2