Click here to Skip to main content
15,915,513 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi to all,

How can I make a Button automatically click every day at 11:00PM using a Timer control?
Posted
Updated 17-May-11 7:27am
v2

OK, I can't really imagine a scenario where you would actually want to click a button at this time, but they are your requirements. There are actually three parts to consider with this question, so I'll cover them one at a time.


  1. When you click a button, you are only calling a method, therefore you don't actually need to worry about the button side - simply call a method containing the logic that your button would be triggering anyway.
  2. The code that runs this should reside on a background thread. In an application that I once wrote that had to wait for a specified time interval, I spawned a background thread - the background thread then calculated the time difference between 11PM and the current time (you might have to do some day manipulation to get the right day to base your calculation on). Then, it simply used a Monitor to wait for that time interval - if the application was being closed, the Monitor was pulsed to wake it up so that it could die gracefully.
  3. The code isn't guaranteed to run at 11PM. It will run as close as it can get to it, but there is no way to guarantee that your code will actually be executed at this time.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-May-11 19:48pm    
Of course this is a bad scenario! (My 5)
All correct.
--SA
Assuming WinForms (as you have not specified)...

You can query DateTime.Now at application startup, calculate the number of milliseconds from that time to 11:00PM and set that as the Interval of your Timer then start it.
In the Tick handler you can call PerformClick() on the Button in question.
 
Share this answer
 

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