2010-03-19 23 views
1

J'ai une tâche de synchronisation très longue qui ne peut pas être interrompue par l'économiseur d'écran ou les modes d'économie d'énergie agressifs. Je veux faire un seul appel api pour arrêter le mode d'économie d'énergie, puis le restaurer une fois la tâche terminée.Arrêt temporaire de l'économiseur d'écran et de l'hibernation dans VB.Net

Le code suivant est composé de plusieurs autres messages, mais il n'a aucun effet sur les paramètres de gestion de l'alimentation de XP. Qu'est-ce que je fais mal? TIA!

Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As Long) As Long 

Public Enum EXECUTION_STATE As Integer 

    ES_CONTINUOUS = &H80000000 
    ES_DISPLAY_REQUIRED = &H2 
    ES_SYSTEM_REQUIRED = &H1 
    ES_AWAYMODE_REQUIRED = &H40 

End Enum 

Public Shared Sub PowerSaveOff() 
    SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS) 
End Sub 

Public Shared Sub PowerSaveOn() 
    SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS) 
End Sub 

Voici l'écran de veille systèmes et paramètres PowerMode: alt text http://img87.imageshack.us/img87/1600/25251376.jpg alt text http://img403.imageshack.us/img403/8145/73347627.jpg

+0

De http://msdn.microsoft.com/en-us/library/aa373208(VS.85).aspx, « Cette fonction ne se limite pas l'économiseur d'écran de l'exécution. " – AMissico

+0

Je pense que vous devez lire la section "Remarques" de http://msdn.microsoft.com/en-us/library/aa373208(VS.85).aspx – AMissico

+1

'ByVal esFlags As Long' =>' ByVal esFlags As EXECUTION_STATE' aiderait avec IntelliSense. – AMissico

Répondre

2

J'ai ajouté EXECUTION_STATE.ES_SYSTEM_REQUIRED, qui "force le système à être en état de fonctionnement en réinitialisant le temporisateur inactif du système" et empêche le système d'entrer dans un état d'économie d'énergie. J'ai également changé la convention d'appel d'API pour utiliser EXECUTION_STATE, enveloppé tout dans une classe d'utilité simple avec de la documentation.

''' <summary> 
''' Simple power manager class that enables applications to inform the system 
''' that it is in use, thereby preventing the system from entering the sleeping 
''' power state or turning off the display while the application is running. 
''' </summary> 
Public Class PowerManager 

#Region " Private Sub New " 
    Private Sub New() 
     'keep compiler from creating default constructor to create utility class 
    End Sub 
#End Region 

    ''' <summary> 
    ''' Enables applications to inform the system that it is in use, thereby preventing the system from entering the sleeping power state or turning off the display while the application is running. 
    ''' </summary> 
    ''' <param name="esFlags">The thread's execution requirements. This parameter can be one or more of the EXECUTION_STATE values.</param> 
    ''' <returns> 
    ''' <para>If the function succeeds, the return value is the previous thread execution state, as a EXECUTION_STATE value.</para> 
    ''' <para>If the function fails, the return value is NULL.</para> 
    '''</returns> 
    ''' <remarks> 
    ''' <para>This function does not stop the screen saver from executing.</para> 
    ''' <para>http://msdn.microsoft.com/en-us/library/aa373208.aspx</para> 
    ''' </remarks> 
    Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As EXECUTION_STATE) As EXECUTION_STATE 

    Public Enum EXECUTION_STATE As Integer 

     ''' <summary> 
     ''' Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared. 
     ''' </summary> 
     ES_CONTINUOUS = &H80000000 

     ''' <summary> 
     ''' Forces the display to be on by resetting the display idle timer. 
     ''' </summary> 
     ES_DISPLAY_REQUIRED = &H2 

     ''' <summary> 
     ''' Forces the system to be in the working state by resetting the system idle timer. 
     ''' </summary> 
     ES_SYSTEM_REQUIRED = &H1 

    End Enum 

    Public Shared Function PowerSaveOff() As EXECUTION_STATE 
     Return SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED Or EXECUTION_STATE.ES_DISPLAY_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS) 
    End Function 

    Public Shared Function PowerSaveOn() As EXECUTION_STATE 
     Return SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS) 
    End Function 

End Class 

Public Class Form1 

    Private _cancel As Boolean 

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 

     'set system standby to one minute 

     _cancel = False 

     PowerManager.PowerSaveOff() 

     Do Until _cancel 
      My.Application.DoEvents() 
     Loop 

     PowerManager.PowerSaveOn() 

     'do not forget to restore your power settings 

    End Sub 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
     _cancel = True 
    End Sub 

End Class 
-1
Microsoft.Win32.RegistryKey rkScreenSaver = 
     Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@" Control Panel\Desktop", true); 
if ((string)rkScreenSaver.GetValue("ScreenSaveActive") == "1") 
{ 
    rkScreenSaver.SetValue("ScreenSaveActive", "0"); 
    rkScreenSaver.Close(); 
} 

Cela devrait fonctionner pour vous.

Sinon, pourquoi ne pas allonger la durée de l'économiseur d'écran à 10 minutes?

+0

Il essayait d'éviter de changer les paramètres d'économiseur d'écran des utilisateurs, mais je pourrais avoir à le faire. Même avec les paramètres d'économiseur d'écran désactivés, j'ai toujours le problème d'hibernation qui est plus problématique pour moi. –

+0

Je vais continuer à chercher si je trouve une solution à la partie d'hibernation. –

+0

La manipulation des paramètres du registre et de l'utilisateur n'est pas une option. – AMissico

1

Cela fonctionne pour moi:

Private Const SPI_SETSCREENSAVEACTIVE = 17 
Private Const SPI_SETSCREENSAVETIMEOUT = 15 
Private Const SPIF_SENDWININICHANGE = &H2 
Private Const SPIF_UPDATEINIFILE = &H1 
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Long, ByVal fuWinIni As Long) As Long 

Private Sub Form_Load() 
Call SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, 0, SPIF_UPDATEINIFILE + SPIF_SENDWININICHANGE) 
End Sub 

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) 
Call SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 1, 0, SPIF_UPDATEINIFILE + SPIF_SENDWININICHANGE) 
End Sub