-1

I want the program to start at 6:40 a.m., but it turns off in a few seconds when it starts.

void setUpTimer(TimeSpan alertTime)
{
  setUpTimer(new TimeSpan(21, 27, 0));
  DateTime current = DateTime.Now;
  TimeSpan timeToGo = alertTime - current.TimeOfDay;

  if (timeToGo < TimeSpan.Zero)
  {
    return; // Time already passed
  }
  timer = new System.Threading.Timer(x =>
  {
    doDiagnosis(); // 
  }, null, timeToGo, Timeout.InfiniteTimeSpan);
}

Dodiagnosis is a self-diagnosis automation method of Korean schools. with Selenium

void doDiagnosis()
{
                firefoxDriver.Navigate().GoToUrl("https://hcs.eduro.go.kr/");
firefoxDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);

  var element = firefoxDriver.FindElementByXPath("//[@id='btnConfirm2']");
  element.Click();

  Thread.Sleep(3000);

  schoolSelect();
  login();
  password();
  conditionCheck();
}
Boris Sokolov
  • 1,723
  • 4
  • 23
  • 38

2 Answers2

1

I don't think Threads are the best solution for this purpose, consider using Cron or a library like Quartz.NET which allows to schedule "jobs" that run at certain times.

Laurent
  • 478
  • 7
  • 15
  • 1
    On Windows OSes you could also use Task Scheduler that's built in. – phuzi Sep 15 '21 at 12:49
  • Absolutely, though it is platform dependant. – Laurent Sep 15 '21 at 13:05
  • Please don't do link-only answers without first putting the salient details in the answer itself. Link only to support your answer, not as the answer itself. – Enigmativity Sep 16 '21 at 22:25
  • And, really, this doesn't answer the question anyway. – Enigmativity Sep 16 '21 at 22:26
  • @Enigmativity I see words around the link though, I don't see why I have to write an essay around the Quartz library when by clicking on the link you can read everything you need to know about it. – Laurent Sep 19 '21 at 14:07
  • @Laurent - This site aims to keep all answers relevant. If the link dies then the answer does too. And also, you should give the OP specific details about quartz that shows them how to use it to answer the question, rather than making them "read everything you need to know". You make their life and all future reader guess how to use the tool. It's not a good answer that way. – Enigmativity Sep 19 '21 at 20:56
  • If the link dies then the library isn't maintained anymore, which would render my answer incorrect anyway. I attempted to answer OP with the best option in my opinion (instead of the other answer who's recommending to use a long running thread, which is not a good idea and unstable, but apparently his answer is fine since you didn't respond to his ?...) – Laurent Sep 20 '21 at 21:13
1

My guess is that the application exits before the timer has elapsed, and then it will obviously not be triggered.

If you have a console application it should be fairly simple to use Async main and use something like this:

await Task.Delay(timeToGo);
doDiagnosis(); 

That should prevent the application from quitting before the event has been processed. But this is assuming you want an application that runs something just once. If you need to run this thing every day you could place the code above in a loop, but it might be better to use the windows task-scheduler to schedule the running of your application.

JonasH
  • 28,608
  • 2
  • 10
  • 23