C# - Windows Forms button function want to run on specified date

I have an application that is windows forms and I was hoping to not have to separate out the manual run logic so having a console type app and forms app.  I want to have a timer on the form it run on specified date
Venkat ManiAnalyst ProgrammerAsked:
Who is Participating?
Éric MoreauSenior .Net ConsultantCommented:
You can have a single application that runs with the UI or just as a task.

You should use the Windows "Task Scheduler" to schedule your app to run .

Have a look at http://emoreau.com/Entries/Articles/2008/11/Passing-arguments-to-an-application-startup.aspx
Shaun VermaakTechnical Specialist/DeveloperCommented:
Or you can change to Windows Service (easiest with TopShelf http://docs.topshelf-project.com/en/latest/configuration/quickstart.html)
Add logic to only run as per schedule
AndyAinscowFreelance programmer / ConsultantCommented:
As you want to have the form / app being used (I was hoping to not have to separate out the manual run logic so having a console type app and forms app) and the form to also run code at a set date and time then the above suggestions are probably not suitable for you.
In the form designer just add a timer to the form, set an interval eg. 30 seconds, and add a handler to the timer tick event to the form.
In the handler check if the date and time has been reached (if(DateTime.Now >= dteTrgt)).  If yes then kill the timer to stop it repeating and run the code you want.  If no then do nothing and try again at the next timer tick.

ps.  The if statement should be a bit more complex using a datetime range rather than just a simple >= check, otherwise it runs everytime the app is started.  An alternative is when the code runs then the next datetime of it to be run is updated and stored somewhere.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Do you intend to leave the app open all the time?  If so, make it "minimize to the tray" with a NotifyIcon:
Eddie ShipmanAll-around developerCommented:
@AndyAinscow, Then the application would have to be running at all times until it is supposed to run.

The best bet is to do 2 things.

Add an optional command line parameter to the application to run the function that is run when the button is pressed.
Then setup a task to run the application passing in the command line parameter to make it run the function.
AndyAinscowFreelance programmer / ConsultantCommented:
Without knowing the precise details of the app it is impossible to say which is the most relevant comment.  In any case the question is solved with one or more of these suggestions.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.