Skip to content

Commit

Permalink
Catch some exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
kkwpsv committed Nov 16, 2021
1 parent e5844c5 commit d53e089
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions WindowDebugger/ViewModels/WindowItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Windows.Media.Imaging;
using static Lsj.Util.Win32.BaseTypes.HRESULT;
using static Lsj.Util.Win32.Constants;
using static Lsj.Util.Win32.Enums.DPI_AWARENESS;
using static Lsj.Util.Win32.Enums.ProcessAccessRights;
using static Lsj.Util.Win32.Enums.RedrawWindowFlags;
using static Lsj.Util.Win32.Enums.ThreadAccessRights;
Expand Down Expand Up @@ -66,7 +67,7 @@ public WindowItem(HWND hwnd)

public ShowWindowCommands WindowShowStates { get => _window.ShowStates; set => _window.ShowStates = value; }

public DPI_AWARENESS DpiAwareness { get => _window.DpiAwareness; }
public DPI_AWARENESS DpiAwareness => GetWithDefaultValueWhenException(() => _window.DpiAwareness, DPI_AWARENESS_UNAWARE);

public IntPtr ParentWindowHandle
{
Expand All @@ -87,7 +88,7 @@ public IntPtr ParentWindowHandle

public bool IsTouchWindow { get => _window.IsTouchWindow; }

public string VirtualDesktopID { get => _window.DesktopID.ToString(); }
public string VirtualDesktopID => GetWithDefaultValueWhenException(() => _window.DesktopID.ToString(), null);

private void SetError()
{
Expand Down Expand Up @@ -248,5 +249,17 @@ private void OnError(SystemErrorCodes? win32ErrorCode, HRESULT? hResult)
ErrorString = Marshal.GetExceptionForHR(hResult.Value).Message;
}
}

private T GetWithDefaultValueWhenException<T>(Func<T> getAction, T defaultValue)
{
try
{
return getAction();
}
catch
{
return default;
}
}
}
}

0 comments on commit d53e089

Please sign in to comment.