Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

i write the below code to restart a system

C#
 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

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.
 
Share this answer
 
Comments
[no name] 10-Dec-13 6:05am    
Thanks Griff..
OriginalGriff 10-Dec-13 6:18am    
You're welcome!
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.
 
Share this answer
 
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    
:)
1st way

Use Threading in your program

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


2nd way

Use Windows service

In Windows Service

C#
Process.Start("path to your program .exe");
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900