Results 1 to 9 of 9

Thread: How to Shutdown PC on Some given Time

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    5

    How to Shutdown PC on Some given Time

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to Shutdown PC on Some given Time

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Lively Member
    Join Date
    Jun 2006
    Posts
    115

    Re: How to Shutdown PC on Some given Time

    I made a shutdown program a while ago here's some of my code to maybe help you out a bit

    VB Code:
    1. Timer1.Start()
    2.         lblCurrentTime.Text = System.DateTime.Now
    3.         If Me.lblCurrentTime.Text.ToString().Equals(Me.dtpTime.Value.ToString()) Then
    4.          
    5.     Private Sub ShutdownAt()
    6.         If Me.cbForce.Checked Then
    7.             Dim processes() As Process = Process.GetProcesses()
    8.             For Each processParent As Process In processes
    9.                 Dim processNames() As Process = Process.GetProcessesByName(processParent.ProcessName)
    10.                 For Each processChild As Process In processNames
    11.  
    12.                     Try
    13.                        
    14.                         If Not processChild.MainWindowTitle.Equals(Me.Text) Then
    15.                             processChild.CloseMainWindow()
    16.                             processChild.Kill()
    17.                             processChild.WaitForExit()
    18.                         End If
    19.                     Catch exception As Exception
    20.  
    21.                     End Try
    22.  
    23.                 Next
    24.             Next
    25.         End If
    26.         System.Windows.Forms.Application.Exit()
    27.         Select Case cbSelect.SelectedIndex
    28.             Case 0
    29.                 Me.Close()
    30.                 WindowsController.ExitWindows(RestartOptions.LogOff, True)
    31.             Case 1
    32.                 Me.Close()
    33.                 WindowsController.ExitWindows(RestartOptions.PowerOff, True)
    34.             Case 2
    35.                 Me.Close()
    36.                 WindowsController.ExitWindows(RestartOptions.Reboot, True)
    37.             Case 3
    38.                 Me.Close()
    39.                 WindowsController.ExitWindows(RestartOptions.ShutDown, True)
    40.             Case 4
    41.                 Me.Close()
    42.                 WindowsController.ExitWindows(RestartOptions.Suspend, True)
    43.             Case 5
    44.                 Me.Close()
    45.                 WindowsController.ExitWindows(RestartOptions.Hibernate, True)
    46.         End Select
    47.         'End If
    48.     End Sub

  4. #4
    Lively Member
    Join Date
    Jun 2006
    Posts
    115

    Re: How to Shutdown PC on Some given Time

    also here's the wondows controler class you need
    Attached Files Attached Files

  5. #5
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    413

    Re: How to Shutdown PC on Some given Time

    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:
    1. If Not processChild.MainWindowTitle.Equals(Me.Text) Then
    2.                             processChild.CloseMainWindow()
    3.                             processChild.Kill()
    4.                             processChild.WaitForExit()
    5.                         End If
    Visual Studio .NET 2005/.NET Framework 2.0

  6. #6
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    Re: How to Shutdown PC on Some given Time

    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.
    Last edited by TokersBall_CDXX; Aug 8th, 2006 at 10:59 AM.
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  7. #7
    Lively Member
    Join Date
    Jun 2006
    Posts
    115

    Re: How to Shutdown PC on Some given Time

    Quote Originally Posted by PENNYSTOCK
    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:
    1. If Not processChild.MainWindowTitle.Equals(Me.Text) Then
    2.                             processChild.CloseMainWindow()
    3.                             processChild.Kill()
    4.                             processChild.WaitForExit()
    5.                         End If
    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 tho

  8. #8
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    413

    Re: How to Shutdown PC on Some given Time

    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.
    VB Code:
    1. WindowsController.ExitWindows(RestartOptions.ShutDown, True)
    When I ran you your code, it crashed my system and I recieved the blue screen of death!


    .
    Visual Studio .NET 2005/.NET Framework 2.0

  9. #9
    Lively Member
    Join Date
    Jun 2006
    Posts
    115

    Re: How to Shutdown PC on Some given Time

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width