Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Which event is to get midnight time in c# winforms? I need to clear all data's in my datagridview after midnight.

What I have tried:

Which event is to get midnight time in c# winforms? I need to clear all data's in my datagridview after midnight.
Posted
Updated 12-Jun-16 20:21pm
Comments
Tomas Takac 13-Jun-16 1:55am    
Are you sure you want to do this from a WinForm app? What if the user logs off and the application will not be running?
Philippe Mori 13-Jun-16 9:02am    
Give useful details to make your question comprehensible and do not copy same text twice. Are there are not such predefined event, the answer would be "None" and would not help you...
BillWoodruff 14-Jun-16 3:37am    
Rahul, check this thread on StackOverFlow out:

http://stackoverflow.com/questions/17428880/how-to-continuously-run-a-c-sharp-console-application-in-background?lq=1

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
C#
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:
C#
if (DateTime.Now > runAt)
   {
   runAt = DateTime.Now.AddDays(1).Date;
   // Do your "Midnight" processing
   }

2) Set up a BackgroundWorker thread:
C#
BackgroundWorker atMidnight = new BackgroundWorker();
atMidnight.WorkerReportsProgress = true;
atMidnight.ProgressChanged += atMidnight_ProgressChanged;
atMidnight.RunWorkerCompleted += atMidnight_RunWorkerCompleted;
atMidnight.DoWork += atMidnight_DoWork;
atMidnight.RunWorkerAsync();

And handle DoWork:
C#
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.
 
Share this answer
 
Comments
BillWoodruff 13-Jun-16 21:22pm    
DateTime dt1 = DateTime.Today.AddDays(1);
DateTime dt2 = DateTime.Now.AddDays(1).Date;

return the same value: midnight of the next day; one less look-up using 'Today :)

however, using DateTime.Now.AddDays(1) is going to return the same time it is now, next day.

what puzzled me scanning your code was that the 'runAt variable is never used outside the foreground thread scope, but perhaps you left that "integration" as an exercise for the OP ?

cheers, Bill
OriginalGriff 14-Jun-16 2:59am    
The runAt was only needed for the Timer version - it has to be at class scope to be persisted from Tick to Tick.
The threaded version uses a different name at method scope so there was less confusion between the two ways of doing it.
I could have used Today, but I prefer to kill "spurious" hours and minutes as the last action - just a personal preference, is all.
BillWoodruff 14-Jun-16 3:24am    
I always learn from your choices ! It occurs to me that procrastination may be defined as "killing time at the last moment possible," but, I'm going to wait a while and think that over :)
The time component of DateTime.Today is the beginning of the current day (i.e. 00:00 hours); by adding 1 day to it, you get the midnight of the current day:
C#
DateTime dt = DateTime.Today.AddDays(1d);

// if (DateTime.Now <= dt) => true
//
// true if the current date-time is less than, or equal to, the midnight of the current day
 
Share this answer
 
v2
Comments
OriginalGriff 14-Jun-16 2:55am    
That's a weird collection of votes you've got there - I added a 5 to give you the full list! :laugh:
BillWoodruff 14-Jun-16 3:30am    
You are being entirely too generous, Sir. I missed the point on this one by a country mile ! If I had "got the point" I might have answered with the idea of creating a Windows recurring Task using the Task Scheduler.

I do think this is the first solution where I have received one each of each kind/value vote; if I could vote for myself, I'd vote #2, and that would be an act of mercy, as well as unhumbled ego :)
OriginalGriff 14-Jun-16 3:40am    
Not at all - the "1" had to be balanced, given it's probably source...:sigh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900