How can I use a timer to schedule exe to run at a specific time

I have a windows service that retrieves date and time from the database and then needs to schedule exe to run at that time. Can someone help?
LVL 35
YZlatAsked:
Who is Participating?
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.

jacobstewartCommented:
Have you tried to create a scheduled task for the exe?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could do something like this:
        private Dictionary<System.Timers.Timer, string> Schedule = new Dictionary<System.Timers.Timer, string>();

        private void ScheduleExe(DateTime LaunchAt, string FullPathToExe)
        {
            TimeSpan ts = LaunchAt.Subtract(DateTime.Now);
            if (ts.TotalMilliseconds > 0)
            {
                System.Timers.Timer timer = new System.Timers.Timer(ts.TotalMilliseconds);
                timer.AutoReset = false;
                timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
                Schedule.Add(timer, FullPathToExe);
                timer.Start();
            }
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            System.Timers.Timer timer = (System.Timers.Timer)sender;
            string FullPathToExe = Schedule[timer];
            Schedule.Remove(timer);
            timer.Dispose();
            System.Diagnostics.Process.Start(FullPathToExe);
        }

Open in new window

0
YZlatAuthor Commented:
idle_mind, what is Schedule in this case?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Mike TomlinsonMiddle School Assistant TeacherCommented:
It's at line #1, and is a Dictionary to associate the string path to the EXE with each Timer instance so you can look it up when the Elapsed() event fires.
0
YZlatAuthor Commented:
I tried your code and it seemed to do nothing. So I che4cked the Task manager and saw that the process for the exe was running but the user form didn't show up. My exe is a Windows application
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Services run in their own "desktop" so you won't see the other exe if it has an interface.  =\

This is not a trivial task:
http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx
http://www.codeproject.com/Articles/36581/Interaction-between-services-and-applications-at-u
0
YZlatAuthor Commented:
Can I add a windows form to my windows service and run that form instead of an exe?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Again, it won't be seen since services run in a different desktop than the user does.  You have to jump thru the hoops to get the GUI visible to the user.  No other way around it...
0
YZlatAuthor Commented:
Thanks.
One more thing, if I replace my Windows service with exe, how can make it run every hour programmatically? I want to schedule it either via code or via setup project i use to install it. Is there a way to do that?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You either:
(1) Run constantly, albeit invisibly, and act accordingly at your desired times.
(2) Use the Windows Scheduler to make it run every hour.

See the answer here:
http://stackoverflow.com/questions/1774764/vista-schedule-task-from-setup
0
YZlatAuthor Commented:
I tried to schedule the task using Windows scheduler but there is no option for hourly, just daily
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hmmm...set it up as "One Time".  Then, in advanced settings, there's an opetion to "Repeat task" where you can set  "Every Hour" and "Indefinitely".
0

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
YZlatAuthor Commented:
Can this be done programmatically?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I've never tried creating a scheduler task via code before...not sure!

There are lots of libraries (free) available that wrap the scheduler API:
http://taskscheduler.codeplex.com/documentation
0
YZlatAuthor Commented:
Thank you, I will check it out
0
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
C#

From novice to tech pro — start learning today.