There is no built in event which is fired at midnight - or any specific time, so you will have to create your own.
There are two ways to handle this:
1) Use a Timer in you app.
Set up a DateTime at class level
private runAt = DateTime.Now.AddDays(1).Date;
Create a Timer, with an interval set to 30000 - that means it will run every 30 seconds.
In the Timer.Tick event handler add:
if (DateTime.Now > runAt)
{
runAt = DateTime.Now.AddDays(1).Date;
}
2) Set up a BackgroundWorker thread:
BackgroundWorker atMidnight = new BackgroundWorker();
atMidnight.WorkerReportsProgress = true;
atMidnight.ProgressChanged += atMidnight_ProgressChanged;
atMidnight.RunWorkerCompleted += atMidnight_RunWorkerCompleted;
atMidnight.DoWork += atMidnight_DoWork;
atMidnight.RunWorkerAsync();
And handle DoWork:
private bool terminate = false;
void atMidnight_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
DateTime reportAt = DateTime.Now.AddDays(1).Date;
while (!terminate)
{
if (DateTime.Now > reportAt)
{
reportAt = DateTime.Now.AddDays(1).Date;
worker.ReportProgress(0);
}
Thread.Sleep(30000);
}
}
You can handle the ProgressChanged event and do your Midnight processing there.