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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

É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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shaun VermaakTechnical SpecialistCommented:
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.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.