• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dapplo / Dapplo.Windows / 24262799127

10 Apr 2026 08:30PM UTC coverage: 32.208% (+0.2%) from 31.968%
24262799127

push

github

web-flow
Merge pull request #55 from dapplo/copilot/add-more-apis-for-system-state

Add Dapplo.Windows.SystemState package for system power state management

607 of 2018 branches covered (30.08%)

Branch coverage included in aggregate %.

41 of 74 new or added lines in 4 files covered. (55.41%)

1 existing line in 1 file now uncovered.

1702 of 5151 relevant lines covered (33.04%)

28.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Dapplo.Windows.SystemState/PowerBroadcastListener.cs
1
// Copyright (c) Dapplo and contributors. All rights reserved.
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3

4
#if !NETSTANDARD2_0
5
using System;
6
using System.Reactive.Linq;
7
using Dapplo.Windows.Messages;
8
using Dapplo.Windows.Messages.Enumerations;
9
using Dapplo.Windows.SystemState.Enums;
10

11
namespace Dapplo.Windows.SystemState;
12

13
/// <summary>
14
/// Provides an observable stream of power broadcast events from the Windows message queue.
15
/// Listens for <see cref="WindowsMessages.WM_POWERBROADCAST"/> messages and exposes them as
16
/// an <see cref="IObservable{T}"/> of <see cref="PowerBroadcastEvent"/> values.
17
/// See <a href="https://learn.microsoft.com/en-us/windows/win32/power/wm-powerbroadcast">WM_POWERBROADCAST message</a>
18
/// and <a href="https://learn.microsoft.com/en-us/windows/win32/power/system-wake-up-events">System Wake-Up Events</a>
19
/// </summary>
20
public static class PowerBroadcastListener
21
{
22
    private static readonly IObservable<PowerBroadcastEvent> _powerBroadcastEvents;
23

24
    static PowerBroadcastListener()
25
    {
NEW
26
        _powerBroadcastEvents = SharedMessageWindow.Messages
×
NEW
27
            .Where(m => m.Msg == WindowsMessages.WM_POWERBROADCAST)
×
NEW
28
            .Select(m => (PowerBroadcastEvent)(uint)m.WParam)
×
NEW
29
            .Publish()
×
NEW
30
            .RefCount();
×
NEW
31
    }
×
32

33
    /// <summary>
34
    /// Gets an observable sequence of power broadcast events.
35
    /// Subscribe to this to be notified of system power state changes such as
36
    /// suspend, resume, battery status changes, etc.
37
    /// </summary>
38
    /// <example>
39
    /// <code>
40
    /// PowerBroadcastListener.PowerEvents
41
    ///     .Where(e => e == PowerBroadcastEvent.PBT_APMRESUMEAUTOMATIC)
42
    ///     .Subscribe(e => Console.WriteLine("System resumed automatically"));
43
    /// </code>
44
    /// </example>
NEW
45
    public static IObservable<PowerBroadcastEvent> PowerEvents => _powerBroadcastEvents;
×
46

47
    /// <summary>
48
    /// Gets an observable sequence that emits when the system is about to suspend.
49
    /// </summary>
50
    public static IObservable<PowerBroadcastEvent> Suspending =>
NEW
51
        _powerBroadcastEvents.Where(e => e == PowerBroadcastEvent.PBT_APMSUSPEND);
×
52

53
    /// <summary>
54
    /// Gets an observable sequence that emits when the system has resumed from suspension.
55
    /// This fires when user activity or an application is detected on the resumed system.
56
    /// </summary>
57
    public static IObservable<PowerBroadcastEvent> ResumedFromSuspend =>
NEW
58
        _powerBroadcastEvents.Where(e => e == PowerBroadcastEvent.PBT_APMRESUMESUSPEND);
×
59

60
    /// <summary>
61
    /// Gets an observable sequence that emits when the system has resumed automatically to handle an event.
62
    /// Applications that receive this event are not allowed to interact with the user.
63
    /// If the user is present, <see cref="ResumedFromSuspend"/> will follow.
64
    /// </summary>
65
    public static IObservable<PowerBroadcastEvent> ResumedAutomatically =>
NEW
66
        _powerBroadcastEvents.Where(e => e == PowerBroadcastEvent.PBT_APMRESUMEAUTOMATIC);
×
67

68
    /// <summary>
69
    /// Gets an observable sequence that emits when the system power status has changed
70
    /// (e.g., switch from battery to AC power, or battery level changed significantly).
71
    /// </summary>
72
    public static IObservable<PowerBroadcastEvent> PowerStatusChanged =>
NEW
73
        _powerBroadcastEvents.Where(e => e == PowerBroadcastEvent.PBT_APMPOWERSTATUSCHANGE);
×
74
}
75
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc