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();
}
CodePudding user response:
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.
CodePudding user response:
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.