1

イベントの抽象化を作成する単純なクラスを作成しましたMicrosoft.Win32.SystemEvents.PowerModeChanged。これは関連するコードです。

Imports Microsoft.Win32

Public Class PowerStateMonitor

Private ReadOnly events As EventHandlerList

Public Custom Event SuspendInitiated As EventHandler(Of EventArgs)

    AddHandler(ByVal value As EventHandler(Of EventArgs))
        Me.events.AddHandler("SuspendInitiatedEvent", value)
    End AddHandler

    RemoveHandler(ByVal value As EventHandler(Of EventArgs))
        Me.events.RemoveHandler("SuspendInitiatedEvent", value)
    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
        Dim handler As EventHandler(Of EventArgs) =
            DirectCast(Me.events("SuspendInitiatedEvent"), 
                       EventHandler(Of EventArgs))

        If (handler IsNot Nothing) Then
            handler.Invoke(sender, e)
        End If
    End RaiseEvent

End Event

Public Custom Event SuspendResumed As EventHandler(Of EventArgs)

    AddHandler(ByVal value As EventHandler(Of EventArgs))
        Me.events.AddHandler("SuspendResumedEvent", value)
    End AddHandler

    RemoveHandler(ByVal value As EventHandler(Of EventArgs))
        Me.events.RemoveHandler("SuspendResumedEvent", value)
    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
        Dim handler As EventHandler(Of EventArgs) =
            DirectCast(Me.events("SuspendResumedEvent"), 
                       EventHandler(Of EventArgs))

        If (handler IsNot Nothing) Then
            handler.Invoke(sender, e)
        End If
    End RaiseEvent

End Event

Public Sub New()
    Me.events = New EventHandlerList
End Sub

Public Overridable Sub Start()
    AddHandler SystemEvents.PowerModeChanged, AddressOf Me.SystemEvents_PowerModeChanged
End Sub

Protected Overridable Sub OnSuspendInitiatedEvent(ByVal e As EventArgs)
    RaiseEvent SuspendInitiated(Me, e)
End Sub

Protected Overridable Sub OnSuspendResumedEvent(ByVal e As EventArgs)
    RaiseEvent SuspendResumed(Me, e)
End Sub

Private Sub SystemEvents_PowerModeChanged(ByVal sender As Object, 
                                          ByVal e As PowerModeChangedEventArgs)

    Select Case e.Mode

        Case PowerModes.Suspend
            Me.OnSuspendInitiatedEvent(Nothing)

        Case PowerModes.Resume
            Me.OnSuspendResumedEvent(Nothing)

    End Select

End Sub

End Class

System.Windows.Forms.Application.SetSuspendState問題は、システムをサスペンドまたはハイバネートするために を呼び出すとSuspendInitiated、クラスのイベントが発生しないことです。以下のコードでサスペンド状態の通知を受けようとしましたが、PowerModeChangedEventArgs.PowerModesプロパティの値が決してそうではないPowerModes.Suspendため、イベントが発生しないため、何も起こりません。

Friend WithEvents PowerStateMon As New PowerStateMonitor

Public Sub Suspend(Optional ByVal force As Boolean = False)

    Application.SetSuspendState(PowerState.Suspend, force:=force, disableWakeEvent:=True)

End Sub

Private Sub PowerStateMon_SuspendInitiated(ByVal sender As Object, ByVal e As EventArgs) _
Handles PowerStateMon.SuspendInitiated

    ' Application.Exit()
    Process.Start("CMD.exe", "/K Echo %TIME% The system is entering in Suspend state.")

End Sub

ただし、クラスの他のイベント はSuspendResumed、サスペンド状態を再開したときに適切に通知します。

Private Sub PowerStateMon_SuspendResumed(ByVal sender As Object, ByVal e As EventArgs) _
Handles PowerStateMon.SuspendResumed

    Process.Start("CMD.exe", "/K Echo %TIME% The system resumed from Suspend state.")

End Sub

MSDN のドキュメントに次のように記載されていることに注意してください。

ただし、アプリケーションがサスペンド要求に応答すると、リソースをクリーンアップしてアクティブなプロセスをシャットダウンするのに必要な時間がかかる場合があります。

私は何が欠けていますか?.

アップデート

通常の動作でシステムをサスペンドすると、つまり Windows のスタートメニューからサスペンドについて通知されることに気付きましたが、メソッドを使用すると通知されません。プロパティSystem.Windows.Forms.Application.SetSuspendStateの値はこれではありませんどうして?PowerModeChangedEventArgs.PowerModesPowerModes.Suspend

4

0 に答える 0