?
Solved

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

Posted on 2012-04-05
15
Medium Priority
?
237 Views
Last Modified: 2012-08-13
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?
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
15 Comments
 
LVL 6

Expert Comment

by:jacobstewart
ID: 37812311
Have you tried to create a scheduled task for the exe?
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1500 total points
ID: 37812461
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
 
LVL 35

Author Comment

by:YZlat
ID: 37812556
idle_mind, what is Schedule in this case?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37812567
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
 
LVL 35

Author Comment

by:YZlat
ID: 37812732
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1500 total points
ID: 37812820
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
 
LVL 35

Author Comment

by:YZlat
ID: 37812832
Can I add a windows form to my windows service and run that form instead of an exe?
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1500 total points
ID: 37812839
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
 
LVL 35

Author Comment

by:YZlat
ID: 37812867
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1500 total points
ID: 37812901
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
 
LVL 35

Author Comment

by:YZlat
ID: 37813186
I tried to schedule the task using Windows scheduler but there is no option for hourly, just daily
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1500 total points
ID: 37813266
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
 
LVL 35

Author Comment

by:YZlat
ID: 37824428
Can this be done programmatically?
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1500 total points
ID: 37824694
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
 
LVL 35

Author Comment

by:YZlat
ID: 37827443
Thank you, I will check it out
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question