locked
Wake PC up from Sleep/Hibernate RRS feed

  • Question

  • Hi @ all,

    i would like to have a function in my program which gives me the possibility to send my PC to sleep and wake him up at a specific time.
    I found a project on codeproject.com:

    http://www.codeproject.com/KB/system/AutomatedPowerMgt.aspx

    It works fine, but i have problems translating it to .Net
    I hope someone can help me pls.

    thx Quesiton-Mark

     

    Wednesday, July 7, 2010 5:59 PM

All replies

  • It is a .Net program - in C# rather than VB but still .Net.

    If you really want it in VB, you code try looking for a conversion tool. Just google "translat c# to vb".

    Not used any of them myself so cannot recommend a specific one.


    Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
    Wednesday, July 7, 2010 6:09 PM
  • yes, sry i wannted to write Basic .Net.
    And i allready used some conversion tool, but most oft the time the translated code doesn't work. (or i'm just unable to do it/make some mistakes)
    Anyway i can't convert the project by my self, so i asked for help.
    Wednesday, July 7, 2010 8:25 PM
  • No one?
    Thursday, July 8, 2010 2:56 PM
  • Hi TheQuestionMark,

    I think you can find all information here

    Thursday, July 8, 2010 3:38 PM
  • Hello TheQuestionMark,
    Take a look at this post. Is it helpful?
    http://vbcity.com/forums/t/139872.aspx
    (system awake)

    Best regards,
    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    Friday, July 9, 2010 6:05 AM
  • Hi,

    here is the code i've converted/reworked - works great for me. (code just uses Sleep no Hibernate, because i don't need it)

    I shortened the code to the basics. So there is no protocol about, what happend.

    Imports System.Runtime.InteropServices
    Imports System.Threading
    
    Public Class Sleep
    
      Private Shared SECONDS As Long = 10000000
      Public Delegate Sub TimerCompleteDelegate()
      Public Event OnTimerCompleted As TimerCompleteDelegate
      Private Shadows handle As IntPtr
    
      <DllImport("kernel32.dll")> _
      Private Shared Function CreateWaitableTimer(ByVal lpTimerAttributes As IntPtr, ByVal bManualReset As Boolean, ByVal lpTimerName As String) As IntPtr
      End Function
    
      <DllImport("kernel32.dll")> _
      Private Shared Function SetWaitableTimer(ByVal hTimer As IntPtr, <[In]()> ByRef ft As Long, ByVal lPeriod As Integer, ByVal pfnCompletionRoutine As TimerCompleteDelegate, ByVal pArgToCompletionRoutine As IntPtr, ByVal fResume As Boolean) As Boolean
      End Function
    
      <DllImport("kernel32.dll")> _
      Private Shared Function SleepEx(ByVal dwMilliseconds As UInteger, ByVal bAlertable As Boolean) As UInteger
      End Function
    
      <DllImport("kernel32.dll", SetLastError:=True, CallingConvention:=CallingConvention.Winapi, CharSet:=CharSet.Auto)> _
      Private Shared Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
      End Function
    
      Private Sub Button_StartSleep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_StartSleep.Click
    
        If Convert.ToInt32(TextBox_SleepDuration.Text) > 0 Then
          SetTimer(Convert.ToInt32(TextBox_SleepDuration.Text), PowerState.Suspend)
        End If
    
      End Sub
    
      Public Sub SetTimer(ByVal Interval As Long, ByVal Power_State As PowerState)
    
        Dim TimerComplete As New TimerCompleteDelegate(AddressOf TimerCompleted)
        Interval = -Interval * SECONDS
        handle = CreateWaitableTimer(IntPtr.Zero, True, "WaitableTimer")
        SetWaitableTimer(handle, Interval, 0, TimerComplete, IntPtr.Zero, True)
    
        Dim Timer_SystemState As New Thread(AddressOf ChangeSystemState)
        Timer_SystemState.Start(Power_State)
    
        Dim Timer_Wait As New Thread(New ThreadStart(AddressOf WaitTimer))
        Timer_Wait.Start()
    
      End Sub
    
      Private Sub ChangeSystemState(ByVal obj As [Object])
        Dim Power_State As PowerState = CType(obj, PowerState)
        System.Windows.Forms.Application.SetSuspendState(Power_State, True, False)
      End Sub
    
      Private Sub WaitTimer()
        SleepEx(&HFFFFFFFFUI, True)
        CloseHandle(handle)
        RaiseEvent OnTimerCompleted()
      End Sub
    
      Private Sub TimerCompleted()
        System.Windows.Forms.Application.Run()
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
        Me.Text = Format(Now, "HH:mm:ss")
    
      End Sub
    
    End Class
    

    If you want to copy this you just need 1 Button (i called it Button_StartSleep) 1 Texbox (TextBox_SleepDuration) and i also added a timer to see if the program runs  after sleep.

    There is just one think i'm working at. I need the pc to wake up at a specific time e.g. 12:00. I mean i just could use timediffrence and convert it to seconds.

    There is just one problem: The sleeptimer sleeps this amount of seconds, but the time the pc needs (like 10-15 seconds for me) to go to sleep mode doesn't count.

     

    Anyone got an idea?

    Question-Mark

    Friday, July 9, 2010 4:09 PM