Click here to Skip to main content
14,644,614 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all

i write the below code to restart a system

 try
            {
if (MessageBox.Show("Are you sure you want to restart your computer?", "Restart",                     MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    ShutDownWindows("2"); // Restart the computer  
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


this i wrote on a button click event.

Now i want is every 1 hour it should automatically execute.i am not looking for any sql solution.

please tell me how to do this..
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 2

I don't know why you would want to do that, but...
You will need to set your app to run automatically on startup (Google will help, it's slightly different for different systems)
Then create a Timer, set its Interval property to 60*60*1000 and handle the Tick event.
Add your code to the handler method.
   
Comments
[no name] 10-Dec-13 6:05am
   
Thanks Griff..
OriginalGriff 10-Dec-13 6:18am
   
You're welcome!
Rate this:
Please Sign up or sign in to vote.

Solution 3

You can add your program to Windows Task Scheduler, where you can select the Time (when to run) and frequency (when to repeat).

So, it will automatically run as per your preference.
Comments
[no name] 10-Dec-13 6:05am
   
Thanks Tadit..
   
Most Welcome buddy. With your acceptance, I reached 50K. :) :)

Thanks a lot Snehasish... :)
Karthik_Mahalingam 10-Dec-13 6:10am
   
congrats tadit
   
Thanks a lot karthik... :) High five. :)
Karthik_Mahalingam 10-Dec-13 6:15am
   
:)
[no name] 10-Dec-13 6:54am
   
Congrats..
   
Thanks a lot ... :)
Rate this:
Please Sign up or sign in to vote.

Solution 1

1st way

Use Threading in your program

Thread.Sleep (TimeSpan.FromHours (1));  // sleep for 1 hour
Thread.Sleep (500);                     // sleep for 500 milliseconds


2nd way

Use Windows service

In Windows Service

Process.Start("path to your program .exe");
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100