Hi,
i want to code this program where i want my PC to shutdown or restart on a given time, is it possible, for e.g i want my PC to restart / shutdown after 5 hours how can i do it.
thanks in advance
Regards
Viking
Printable View
Hi,
i want to code this program where i want my PC to shutdown or restart on a given time, is it possible, for e.g i want my PC to restart / shutdown after 5 hours how can i do it.
thanks in advance
Regards
Viking
Create a Timer and set its Interval to the number of milliseconds until the desired shutdown time. In the Tick event handler execute the required code to shutdown the machine. There are various ways to do that, e.g, shutdown.exe and the ExitWindowsEx API function, and they have been posted about many times before so a forum search will give you what you need there.
I made a shutdown program a while ago here's some of my code to maybe help you out a bit
VB Code:
Timer1.Start() lblCurrentTime.Text = System.DateTime.Now If Me.lblCurrentTime.Text.ToString().Equals(Me.dtpTime.Value.ToString()) Then Private Sub ShutdownAt() If Me.cbForce.Checked Then Dim processes() As Process = Process.GetProcesses() For Each processParent As Process In processes Dim processNames() As Process = Process.GetProcessesByName(processParent.ProcessName) For Each processChild As Process In processNames Try If Not processChild.MainWindowTitle.Equals(Me.Text) Then processChild.CloseMainWindow() processChild.Kill() processChild.WaitForExit() End If Catch exception As Exception End Try Next Next End If System.Windows.Forms.Application.Exit() Select Case cbSelect.SelectedIndex Case 0 Me.Close() WindowsController.ExitWindows(RestartOptions.LogOff, True) Case 1 Me.Close() WindowsController.ExitWindows(RestartOptions.PowerOff, True) Case 2 Me.Close() WindowsController.ExitWindows(RestartOptions.Reboot, True) Case 3 Me.Close() WindowsController.ExitWindows(RestartOptions.ShutDown, True) Case 4 Me.Close() WindowsController.ExitWindows(RestartOptions.Suspend, True) Case 5 Me.Close() WindowsController.ExitWindows(RestartOptions.Hibernate, True) End Select 'End If End Sub
also here's the wondows controler class you need
Where is the heart here. Let a novice crash his computer like that.
Good shut down procudure. NOT!!!! Kill all your processes is bad bad bad!!
VB Code:
If Not processChild.MainWindowTitle.Equals(Me.Text) Then processChild.CloseMainWindow() processChild.Kill() processChild.WaitForExit() End If
isn't that what shutting down your pc does? terminate all active processes?
certaintly it's alot cleaner to close them yourself before calling windows to shutdown.
just food for thought though.
I put if cbForce is checked if you check it then you choose the option for force shutdown to close out all open program's if not leave it unchecked to just shutdown the basic way it's just a option thoQuote:
Originally Posted by PENNYSTOCK
I haven't tried in extesivly, but the second parameter of this tells the shutdown to force it anyway. However, it give the process at least a chance.When I ran you your code, it crashed my system and I recieved the blue screen of death!VB Code:
WindowsController.ExitWindows(RestartOptions.ShutDown, True)
.
well the program that I made I never got the blue screen of death and the ppl i've had test it haven't either so i'm not sure what the problem maybe