Annotate Microsoft.Win32.SystemEvents for nullable reference types (#1621)
authorStephen Toub <stoub@microsoft.com>
Sat, 11 Jan 2020 18:19:08 +0000 (13:19 -0500)
committerGitHub <noreply@github.com>
Sat, 11 Jan 2020 18:19:08 +0000 (13:19 -0500)
* Annotate Microsoft.Win32.SystemEvents for nullable reference types

* Address PR feedback

src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetModuleHandle.cs
src/libraries/Microsoft.Win32.SystemEvents/ref/Microsoft.Win32.SystemEvents.cs
src/libraries/Microsoft.Win32.SystemEvents/ref/Microsoft.Win32.SystemEvents.csproj
src/libraries/Microsoft.Win32.SystemEvents/src/Microsoft.Win32.SystemEvents.csproj
src/libraries/Microsoft.Win32.SystemEvents/src/Microsoft/Win32/SystemEvents.cs
src/libraries/Microsoft.Win32.SystemEvents/tests/GenericEventTests.cs
src/libraries/Microsoft.Win32.SystemEvents/tests/SystemEvents.DisplaySettings.cs

index ff64bc2..d30358d 100644 (file)
@@ -2,16 +2,15 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using Microsoft.Win32.SafeHandles;
+#nullable enable
 using System;
 using System.Runtime.InteropServices;
-using System.Text;
 
 internal partial class Interop
 {
     internal partial class Kernel32
     {
         [DllImport(Libraries.Kernel32, EntryPoint = "GetModuleHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
-        internal static extern IntPtr GetModuleHandle(string moduleName);
+        internal static extern IntPtr GetModuleHandle(string? moduleName);
     }
 }
index 1663b48..a1bfcd9 100644 (file)
@@ -58,23 +58,23 @@ namespace Microsoft.Win32
     public sealed partial class SystemEvents
     {
         internal SystemEvents() { }
-        public static event System.EventHandler DisplaySettingsChanged { add { } remove { } }
-        public static event System.EventHandler DisplaySettingsChanging { add { } remove { } }
-        public static event System.EventHandler EventsThreadShutdown { add { } remove { } }
-        public static event System.EventHandler InstalledFontsChanged { add { } remove { } }
+        public static event System.EventHandler? DisplaySettingsChanged { add { } remove { } }
+        public static event System.EventHandler? DisplaySettingsChanging { add { } remove { } }
+        public static event System.EventHandler? EventsThreadShutdown { add { } remove { } }
+        public static event System.EventHandler? InstalledFontsChanged { add { } remove { } }
         [System.ComponentModel.BrowsableAttribute(false)]
         [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
         [System.ObsoleteAttribute("This event has been deprecated. https://go.microsoft.com/fwlink/?linkid=14202")]
-        public static event System.EventHandler LowMemory { add { } remove { } }
-        public static event System.EventHandler PaletteChanged { add { } remove { } }
-        public static event Microsoft.Win32.PowerModeChangedEventHandler PowerModeChanged { add { } remove { } }
-        public static event Microsoft.Win32.SessionEndedEventHandler SessionEnded { add { } remove { } }
-        public static event Microsoft.Win32.SessionEndingEventHandler SessionEnding { add { } remove { } }
-        public static event Microsoft.Win32.SessionSwitchEventHandler SessionSwitch { add { } remove { } }
-        public static event System.EventHandler TimeChanged { add { } remove { } }
-        public static event Microsoft.Win32.TimerElapsedEventHandler TimerElapsed { add { } remove { } }
-        public static event Microsoft.Win32.UserPreferenceChangedEventHandler UserPreferenceChanged { add { } remove { } }
-        public static event Microsoft.Win32.UserPreferenceChangingEventHandler UserPreferenceChanging { add { } remove { } }
+        public static event System.EventHandler? LowMemory { add { } remove { } }
+        public static event System.EventHandler? PaletteChanged { add { } remove { } }
+        public static event Microsoft.Win32.PowerModeChangedEventHandler? PowerModeChanged { add { } remove { } }
+        public static event Microsoft.Win32.SessionEndedEventHandler? SessionEnded { add { } remove { } }
+        public static event Microsoft.Win32.SessionEndingEventHandler? SessionEnding { add { } remove { } }
+        public static event Microsoft.Win32.SessionSwitchEventHandler? SessionSwitch { add { } remove { } }
+        public static event System.EventHandler? TimeChanged { add { } remove { } }
+        public static event Microsoft.Win32.TimerElapsedEventHandler? TimerElapsed { add { } remove { } }
+        public static event Microsoft.Win32.UserPreferenceChangedEventHandler? UserPreferenceChanged { add { } remove { } }
+        public static event Microsoft.Win32.UserPreferenceChangingEventHandler? UserPreferenceChanging { add { } remove { } }
         public static System.IntPtr CreateTimer(int interval) { throw null; }
         public static void InvokeOnEventsThread(System.Delegate method) { }
         public static void KillTimer(System.IntPtr timerId) { }
index 5617ada..f3fef51 100644 (file)
@@ -1,6 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <IsPartialFacadeAssembly Condition="'$(TargetsNetFx)' == 'true'">true</IsPartialFacadeAssembly>
+    <Nullable>enable</Nullable>
     <Configurations>net461-Debug;net461-Release;$(NetFrameworkCurrent)-Debug;$(NetFrameworkCurrent)-Release;netstandard2.0-Debug;netstandard2.0-Release</Configurations>
   </PropertyGroup>
   <ItemGroup>
index f3f6dd9..a94653c 100644 (file)
@@ -2,6 +2,7 @@
   <PropertyGroup>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <IsPartialFacadeAssembly Condition="'$(TargetsNetFx)' == 'true'">true</IsPartialFacadeAssembly>
+    <Nullable>enable</Nullable>
     <GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetsNetStandard)' == 'true' and '$(OSGroup)' == 'AnyOS'">SR.PlatformNotSupported_SystemEvents</GeneratePlatformNotSupportedAssemblyMessage>
     <Configurations>net461-Debug;net461-Release;$(NetCoreAppCurrent)-Windows_NT-Debug;$(NetCoreAppCurrent)-Windows_NT-Release;netcoreapp2.0-Windows_NT-Debug;netcoreapp2.0-Windows_NT-Release;netcoreapp3.0-Windows_NT-Debug;netcoreapp3.0-Windows_NT-Release;$(NetFrameworkCurrent)-Debug;$(NetFrameworkCurrent)-Release;netstandard2.0-Debug;netstandard2.0-Release</Configurations>
   </PropertyGroup>
index ca6c3b5..0fb8034 100644 (file)
@@ -24,24 +24,24 @@ namespace Microsoft.Win32
         // one of the delegates.
         private static readonly object s_eventLockObject = new object();
         private static readonly object s_procLockObject = new object();
-        private static volatile SystemEvents s_systemEvents;
-        private static volatile Thread s_windowThread;
-        private static volatile ManualResetEvent s_eventWindowReady;
+        private static volatile SystemEvents? s_systemEvents;
+        private static volatile Thread? s_windowThread;
+        private static volatile ManualResetEvent? s_eventWindowReady;
         private static readonly Random s_randomTimerId = new Random();
         private static volatile bool s_registeredSessionNotification = false;
         private static volatile IntPtr s_defWindowProc;
 
-        private static volatile string s_className = null;
+        private static volatile string? s_className;
 
         // cross-thread marshaling
-        private static volatile Queue<Delegate> s_threadCallbackList; // list of Delegates
+        private static volatile Queue<Delegate>? s_threadCallbackList; // list of Delegates
         private static volatile int s_threadCallbackMessage = 0;
-        private static volatile ManualResetEvent s_eventThreadTerminated;
+        private static volatile ManualResetEvent? s_eventThreadTerminated;
 
         // Per-instance data that is isolated to the window thread.
         private volatile IntPtr _windowHandle;
-        private Interop.User32.WndProc _windowProc;
-        private Interop.Kernel32.ConsoleCtrlHandlerRoutine _consoleHandler;
+        private Interop.User32.WndProc? _windowProc;
+        private Interop.Kernel32.ConsoleCtrlHandlerRoutine? _consoleHandler;
 
         // The set of events we respond to.
         private static readonly object s_onUserPreferenceChangingEvent = new object();
@@ -61,7 +61,7 @@ namespace Microsoft.Win32
 
         // Our list of handler information.  This is a lookup of the above keys and objects that
         // match a delegate with a SynchronizationContext so we can fire on the proper thread.
-        private static Dictionary<object, List<SystemEventInvokeInfo>> s_handlers;
+        private static Dictionary<object, List<SystemEventInvokeInfo>>? s_handlers;
 
         private SystemEvents()
         {
@@ -102,7 +102,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///  Occurs when the display settings are changing.
         /// </summary>
-        public static event EventHandler DisplaySettingsChanging
+        public static event EventHandler? DisplaySettingsChanging
         {
             add
             {
@@ -117,7 +117,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///  Occurs when the user changes the display settings.
         /// </summary>
-        public static event EventHandler DisplaySettingsChanged
+        public static event EventHandler? DisplaySettingsChanged
         {
             add
             {
@@ -133,7 +133,7 @@ namespace Microsoft.Win32
         ///  Occurs before the thread that listens for system events is terminated.
         ///  Delegates will be invoked on the events thread.
         /// </summary>
-        public static event EventHandler EventsThreadShutdown
+        public static event EventHandler? EventsThreadShutdown
         {
             // Really only here for GDI+ initialization and shut down
             add
@@ -149,7 +149,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///  Occurs when the user adds fonts to or removes fonts from the system.
         /// </summary>
-        public static event EventHandler InstalledFontsChanged
+        public static event EventHandler? InstalledFontsChanged
         {
             add
             {
@@ -166,7 +166,7 @@ namespace Microsoft.Win32
         /// </summary>
         [Obsolete("This event has been deprecated. https://go.microsoft.com/fwlink/?linkid=14202")]
         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
-        public static event EventHandler LowMemory
+        public static event EventHandler? LowMemory
         {
             add
             {
@@ -183,7 +183,7 @@ namespace Microsoft.Win32
         ///  Occurs when the user switches to an application that uses a different
         ///  palette.
         /// </summary>
-        public static event EventHandler PaletteChanged
+        public static event EventHandler? PaletteChanged
         {
             add
             {
@@ -199,7 +199,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///  Occurs when the user suspends or resumes the system.
         /// </summary>
-        public static event PowerModeChangedEventHandler PowerModeChanged
+        public static event PowerModeChangedEventHandler? PowerModeChanged
         {
             add
             {
@@ -215,7 +215,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///  Occurs when the user is logging off or shutting down the system.
         /// </summary>
-        public static event SessionEndedEventHandler SessionEnded
+        public static event SessionEndedEventHandler? SessionEnded
         {
             add
             {
@@ -231,7 +231,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///  Occurs when the user is trying to log off or shutdown the system.
         /// </summary>
-        public static event SessionEndingEventHandler SessionEnding
+        public static event SessionEndingEventHandler? SessionEnding
         {
             add
             {
@@ -247,7 +247,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///  Occurs when a user session switches.
         /// </summary>
-        public static event SessionSwitchEventHandler SessionSwitch
+        public static event SessionSwitchEventHandler? SessionSwitch
         {
             add
             {
@@ -264,7 +264,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///   Occurs when the user changes the time on the system clock.
         /// </summary>
-        public static event EventHandler TimeChanged
+        public static event EventHandler? TimeChanged
         {
             add
             {
@@ -280,7 +280,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///  Occurs when a windows timer interval has expired.
         /// </summary>
-        public static event TimerElapsedEventHandler TimerElapsed
+        public static event TimerElapsedEventHandler? TimerElapsed
         {
             add
             {
@@ -297,7 +297,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///  Occurs when a user preference has changed.
         /// </summary>
-        public static event UserPreferenceChangedEventHandler UserPreferenceChanged
+        public static event UserPreferenceChangedEventHandler? UserPreferenceChanged
         {
             add
             {
@@ -312,7 +312,7 @@ namespace Microsoft.Win32
         /// <summary>
         ///  Occurs when a user preference is changing.
         /// </summary>
-        public static event UserPreferenceChangingEventHandler UserPreferenceChanging
+        public static event UserPreferenceChangingEventHandler? UserPreferenceChanging
         {
             add
             {
@@ -324,8 +324,13 @@ namespace Microsoft.Win32
             }
         }
 
-        private static void AddEventHandler(object key, Delegate value)
+        private static void AddEventHandler(object key, Delegate? value)
         {
+            if (value is null)
+            {
+                return;
+            }
+
             lock (s_eventLockObject)
             {
                 if (s_handlers == null)
@@ -334,7 +339,7 @@ namespace Microsoft.Win32
                     EnsureSystemEvents(requireHandle: false);
                 }
 
-                if (!s_handlers.TryGetValue(key, out List<SystemEventInvokeInfo> invokeItems))
+                if (!s_handlers.TryGetValue(key, out List<SystemEventInvokeInfo>? invokeItems))
                 {
                     invokeItems = new List<SystemEventInvokeInfo>();
                     s_handlers[key] = invokeItems;
@@ -391,7 +396,7 @@ namespace Microsoft.Win32
             }
 
             EnsureSystemEvents(requireHandle: true);
-            IntPtr timerId = Interop.User32.SendMessageW(new HandleRef(s_systemEvents, s_systemEvents._windowHandle),
+            IntPtr timerId = Interop.User32.SendMessageW(new HandleRef(s_systemEvents, s_systemEvents!._windowHandle),
                                                         Interop.User32.WM_CREATETIMER, (IntPtr)interval, IntPtr.Zero);
 
             if (timerId == IntPtr.Zero)
@@ -407,7 +412,7 @@ namespace Microsoft.Win32
             {
                 if (s_registeredSessionNotification)
                 {
-                    Interop.Wtsapi32.WTSUnRegisterSessionNotification(new HandleRef(s_systemEvents, s_systemEvents._windowHandle));
+                    Interop.Wtsapi32.WTSUnRegisterSessionNotification(new HandleRef(s_systemEvents, s_systemEvents!._windowHandle));
                 }
 
                 IntPtr handle = _windowHandle;
@@ -444,7 +449,7 @@ namespace Microsoft.Win32
                 else
                 {
                     IntPtr hInstance = Interop.Kernel32.GetModuleHandle(null);
-                    Interop.User32.UnregisterClassW(s_className, hInstance);
+                    Interop.User32.UnregisterClassW(s_className!, hInstance);
                 }
             }
 
@@ -515,7 +520,7 @@ namespace Microsoft.Win32
 
                 if (retval != IntPtr.Zero)
                 {
-                    Interop.Wtsapi32.WTSRegisterSessionNotification(new HandleRef(s_systemEvents, s_systemEvents._windowHandle), Interop.Wtsapi32.NOTIFY_FOR_THIS_SESSION);
+                    Interop.Wtsapi32.WTSRegisterSessionNotification(new HandleRef(s_systemEvents, s_systemEvents!._windowHandle), Interop.Wtsapi32.NOTIFY_FOR_THIS_SESSION);
                     s_registeredSessionNotification = true;
                     Interop.Kernel32.FreeLibrary(retval);
                 }
@@ -528,11 +533,11 @@ namespace Microsoft.Win32
 
             if (msg == Interop.User32.WM_SETTINGCHANGE)
             {
-                if (lParam != IntPtr.Zero && Marshal.PtrToStringUni(lParam).Equals("Policy"))
+                if (lParam != IntPtr.Zero && Marshal.PtrToStringUni(lParam)!.Equals("Policy"))
                 {
                     pref = UserPreferenceCategory.Policy;
                 }
-                else if (lParam != IntPtr.Zero && Marshal.PtrToStringUni(lParam).Equals("intl"))
+                else if (lParam != IntPtr.Zero && Marshal.PtrToStringUni(lParam)!.Equals("intl"))
                 {
                     pref = UserPreferenceCategory.Locale;
                 }
@@ -716,12 +721,12 @@ namespace Microsoft.Win32
         {
             Debug.Assert(s_threadCallbackList != null, "Invoking marshaled callbacks before there are any");
 
-            Delegate current = null;
-            lock (s_threadCallbackList)
+            Delegate? current = null;
+            lock (s_threadCallbackList!)
             {
                 if (s_threadCallbackList.Count > 0)
                 {
-                    current = (Delegate)s_threadCallbackList.Dequeue();
+                    current = s_threadCallbackList.Dequeue();
                 }
             }
 
@@ -732,8 +737,7 @@ namespace Microsoft.Win32
                 {
                     // Optimize a common case of using EventHandler. This allows us to invoke
                     // early bound, which is a bit more efficient.
-                    EventHandler c = current as EventHandler;
-                    if (c != null)
+                    if (current is EventHandler c)
                     {
                         c(null, EventArgs.Empty);
                     }
@@ -746,16 +750,12 @@ namespace Microsoft.Win32
                 {
                     Debug.Fail("SystemEvents marshaled callback failed:" + t);
                 }
+
                 lock (s_threadCallbackList)
                 {
-                    if (s_threadCallbackList.Count > 0)
-                    {
-                        current = (Delegate)s_threadCallbackList.Dequeue();
-                    }
-                    else
-                    {
-                        current = null;
-                    }
+                    current = s_threadCallbackList.Count > 0 ?
+                        s_threadCallbackList.Dequeue() :
+                        null;
                 }
             }
         }
@@ -770,7 +770,7 @@ namespace Microsoft.Win32
 
 #if DEBUG
             int pid;
-            int thread = Interop.User32.GetWindowThreadProcessId(new HandleRef(s_systemEvents, s_systemEvents._windowHandle), out pid);
+            int thread = Interop.User32.GetWindowThreadProcessId(new HandleRef(s_systemEvents, s_systemEvents!._windowHandle), out pid);
             Debug.Assert(s_windowThread == null || thread != Interop.Kernel32.GetCurrentThreadId(), "Don't call MarshaledInvoke on the system events thread");
 #endif
 
@@ -793,7 +793,7 @@ namespace Microsoft.Win32
                 s_threadCallbackList.Enqueue(method);
             }
 
-            Interop.User32.PostMessageW(new HandleRef(s_systemEvents, s_systemEvents._windowHandle), s_threadCallbackMessage, IntPtr.Zero, IntPtr.Zero);
+            Interop.User32.PostMessageW(new HandleRef(s_systemEvents, s_systemEvents!._windowHandle), s_threadCallbackMessage, IntPtr.Zero, IntPtr.Zero);
         }
 
         /// <summary>
@@ -802,7 +802,7 @@ namespace Microsoft.Win32
         public static void KillTimer(IntPtr timerId)
         {
             EnsureSystemEvents(requireHandle: true);
-            if (s_systemEvents._windowHandle != IntPtr.Zero)
+            if (s_systemEvents!._windowHandle != IntPtr.Zero)
             {
                 int res = (int)Interop.User32.SendMessageW(new HandleRef(s_systemEvents, s_systemEvents._windowHandle),
                                                                 Interop.User32.WM_KILLTIMER, timerId, IntPtr.Zero);
@@ -1002,13 +1002,15 @@ namespace Microsoft.Win32
         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
         private static void RaiseEvent(bool checkFinalization, object key, params object[] args)
         {
+            Debug.Assert(args != null && args.Length == 2);
+
             // If the AppDomain's unloading, we shouldn't fire SystemEvents other than Shutdown.
             if (checkFinalization && AppDomain.CurrentDomain.IsFinalizingForUnload())
             {
                 return;
             }
 
-            SystemEventInvokeInfo[] invokeItemArray = null;
+            SystemEventInvokeInfo?[]? invokeItemArray = null;
 
             lock (s_eventLockObject)
             {
@@ -1031,8 +1033,8 @@ namespace Microsoft.Win32
                 {
                     try
                     {
-                        SystemEventInvokeInfo info = invokeItemArray[i];
-                        info.Invoke(checkFinalization, args);
+                        SystemEventInvokeInfo info = invokeItemArray[i]!;
+                        info.Invoke(checkFinalization, args!);
                         invokeItemArray[i] = null; // clear it if it's valid
                     }
                     catch (Exception)
@@ -1044,16 +1046,16 @@ namespace Microsoft.Win32
                 // clean out any that are dead.
                 lock (s_eventLockObject)
                 {
-                    List<SystemEventInvokeInfo> invokeItems = null;
+                    List<SystemEventInvokeInfo>? invokeItems = null;
 
                     for (int i = 0; i < invokeItemArray.Length; i++)
                     {
-                        SystemEventInvokeInfo info = invokeItemArray[i];
+                        SystemEventInvokeInfo? info = invokeItemArray[i];
                         if (info != null)
                         {
                             if (invokeItems == null)
                             {
-                                if (!s_handlers.TryGetValue(key, out invokeItems))
+                                if (!s_handlers!.TryGetValue(key, out invokeItems))
                                 {
                                     // weird.  just to be safe.
                                     return;
@@ -1067,13 +1069,18 @@ namespace Microsoft.Win32
             }
         }
 
-        private static void RemoveEventHandler(object key, Delegate value)
+        private static void RemoveEventHandler(object key, Delegate? value)
         {
+            if (value is null)
+            {
+                return;
+            }
+
             lock (s_eventLockObject)
             {
                 if (s_handlers != null && s_handlers.ContainsKey(key))
                 {
-                    List<SystemEventInvokeInfo> invokeItems = (List<SystemEventInvokeInfo>)s_handlers[key];
+                    List<SystemEventInvokeInfo> invokeItems = s_handlers[key];
 
                     invokeItems.Remove(new SystemEventInvokeInfo(value));
                 }
@@ -1114,7 +1121,7 @@ namespace Microsoft.Win32
         }
 
         [PrePrepareMethod]
-        private static void Shutdown(object sender, EventArgs e)
+        private static void Shutdown(object? sender, EventArgs e)
         {
             Shutdown();
         }
@@ -1129,7 +1136,7 @@ namespace Microsoft.Win32
             switch (msg)
             {
                 case Interop.User32.WM_SETTINGCHANGE:
-                    string newString;
+                    string? newString;
                     IntPtr newStringPtr = lParam;
                     if (lParam != IntPtr.Zero)
                     {
@@ -1255,7 +1262,7 @@ namespace Microsoft.Win32
             try
             {
                 Initialize();
-                s_eventWindowReady.Set();
+                s_eventWindowReady!.Set();
 
                 if (_windowHandle != IntPtr.Zero)
                 {
@@ -1297,7 +1304,7 @@ namespace Microsoft.Win32
             {
                 // In case something very very wrong happend during the creation action.
                 // This will unblock the calling thread.
-                s_eventWindowReady.Set();
+                s_eventWindowReady!.Set();
 
                 if (!((e is ThreadInterruptedException) || (e is ThreadAbortException)))
                 {
@@ -1351,20 +1358,14 @@ namespace Microsoft.Win32
             }
 
             // our delegate method that the SyncContext will call on.
-            private void InvokeCallback(object arg)
+            private void InvokeCallback(object? arg)
             {
-                _delegate.DynamicInvoke((object[])arg);
+                _delegate.DynamicInvoke((object[]?)arg);
             }
 
-            public override bool Equals(object other)
+            public override bool Equals(object? other)
             {
-                SystemEventInvokeInfo otherInvoke = other as SystemEventInvokeInfo;
-
-                if (otherInvoke == null)
-                {
-                    return false;
-                }
-                return otherInvoke._delegate.Equals(_delegate);
+                return other is SystemEventInvokeInfo otherInvoke && otherInvoke._delegate.Equals(_delegate);
             }
 
             public override int GetHashCode()
index 8a5098b..0b04700 100644 (file)
@@ -28,7 +28,7 @@ namespace Microsoft.Win32.SystemEventsTests
         public void SignalsEventsAsynchronouslyOnMessage()
         {
             var signal = new AutoResetEvent(false);
-            EventHandler signaledHandler = (o, e) => signal.Set();
+            EventHandler signaledHandler = (o, e) => { Assert.NotNull(o); signal.Set(); };
 
             Event += signaledHandler;
 
@@ -48,7 +48,7 @@ namespace Microsoft.Win32.SystemEventsTests
         public void SignalsEventsSynchronouslyOnReflectedMessage()
         {
             bool signal = false;
-            EventHandler signaledHandler = (o, e) => signal = true;
+            EventHandler signaledHandler = (o, e) => { Assert.NotNull(o); signal = true; };
 
             Event += signaledHandler;
 
index 1628a83..f68da3a 100644 (file)
@@ -25,8 +25,8 @@ namespace Microsoft.Win32.SystemEventsTests
         {
             var changing = new AutoResetEvent(false);
             var changed = new AutoResetEvent(false);
-            EventHandler changedHandler = (o, e) => changed.Set();
-            EventHandler changingHandler = (o, e) => changing.Set();
+            EventHandler changedHandler = (o, e) => { Assert.NotNull(o); changed.Set(); };
+            EventHandler changingHandler = (o, e) => { Assert.NotNull(o); changing.Set(); };
 
             SystemEvents.DisplaySettingsChanged += changedHandler;
             SystemEvents.DisplaySettingsChanging += changingHandler;
@@ -50,8 +50,8 @@ namespace Microsoft.Win32.SystemEventsTests
         public void SignalsDisplayEventsSynchronouslyOnREFLECTDISPLAYCHANGE()
         {
             bool changing = false, changed = false;
-            EventHandler changedHandler = (o, e) => changed = true;
-            EventHandler changingHandler = (o, e) => changing = true;
+            EventHandler changedHandler = (o, e) => { Assert.NotNull(o); changed = true; };
+            EventHandler changingHandler = (o, e) => { Assert.NotNull(o); changing = true; };
 
             SystemEvents.DisplaySettingsChanged += changedHandler;
             SystemEvents.DisplaySettingsChanging += changingHandler;