0

I want the user to be able to chose the time the ClearFiles() function will get called every day. The timer executes at the time the user selects but never correctly fires after the x amount of minutes / hours specified.

Code:

DateTime nowTime = DateTime.Now;
DateTime deleteTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day,
    FileFilter.deleteTime.Hour, FileFilter.deleteTime.Minute, 0);
if (nowTime > deleteTime)
{
    deleteTime = deleteTime.AddDays(1)
}

double tickTime = (deleteTime - nowTime).TotalMilliseconds;
aTimer = new Timer(tickTime);
aTimer.Elapsed += (s, e) => ClearFiles(FileFilter.pathName);
aTimer.Start();
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • What do you mean by "never correctly fires"? Does it fire period? If it does, what does it do and what are you expecting it to do? Just glancing over this code really quickly, I don't see anything wrong with it. – Jesse Aug 29 '22 at 22:57
  • 3
    Probably should be using something like [Task scheduler](https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page) for this sort of requirement. Expecting an executing process to keep a timer that long in memory is a very brittle design. – John Wu Aug 29 '22 at 23:00
  • @jesse Say I set the timer to call the function at 4:30 it will call that function at 4:30. The problem is if I set ( deleteTime = deleteTime.AddMinutes(20) ) it wont fire at 4:50, it will go off quite a bit earlier than I want it to. – nathan10802 Aug 29 '22 at 23:02
  • @JohnWu That's how the place I work for has done it in the past but they want me to move away from the Task scheduler and do it using Blazor. – nathan10802 Aug 29 '22 at 23:04
  • 3
    That strikes me as absurd. I don't use that word lightly. Blazor is a tool for building UIs, not for executing unattended (I assume nobody is going to sit there for 24 hours) cleanup tasks. Best of luck to you! – John Wu Aug 29 '22 at 23:09
  • @John Wu I agree, but its to delete files that our software creates from scanning things, which add up pretty quickly if they are scanning all day. – nathan10802 Aug 29 '22 at 23:15
  • With the information provided I don't see any reason that issue would come up as long as you're adding the time correctly and in the correct circumstances. To add onto what John said, the company I work for does something similar to this. The ASP.NET context is not a great place to create recurring tasks (I've tried it before. It's not a fun process). For this reason, we offload all of our jobs to [Hangfire](https://www.hangfire.io/) and I would recommend you try the same thing (or something similar). – Jesse Aug 29 '22 at 23:17
  • Please [edit] code and show the missing part where you schedule "every day" timer. It would not make idea what you trying to do better, but at least someone can answer the precise question you have - so far there is no way to know why code *not shown in the post* is not working correctly. Re-read [mre] guidance to know what code to keep/add to the post. – Alexei Levenkov Aug 29 '22 at 23:23
  • @AlexeiLevenkov the deleteTime = deleteTime.AddDays(1) was the part I was hoping would add 24 hours to the timer. – nathan10802 Aug 29 '22 at 23:28
  • @nathan10802 please find rubber duck and explain it your code. I.e. I read the code/question as: "I set timer to repeat every 7 minutes, why it does not go off every day when some unrelated part of my code has `AddDay(1)`" . That is why SO requires *you* as the question's author to create [mre] - remove all unrelated pieces and inline all values as constants where possible... If you still have a problem after explaining the code *in this question* (and not one in your head) to the duck - [edit] the question and it likely will be clear and useful to others. – Alexei Levenkov Aug 30 '22 at 00:29
  • Related: [How to start my Worker Service on the hour](https://stackoverflow.com/questions/71883959/how-to-start-my-worker-service-on-the-hour). – Theodor Zoulias Aug 30 '22 at 00:43
  • I second John Wu's comments. Best of luck! – MrC aka Shaun Curtis Aug 31 '22 at 10:40

0 Answers0