Skip to content

Commit d1af645

Browse files
authored
Add version check before accessing HasThreadAccess property (#6176) (#6180)
1 parent 7e6462c commit d1af645

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Files/Helpers/NativeWinApiHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,21 @@ private static bool IsArmProcessor()
233233
nativeMachine == 0x01c4);
234234
}
235235

236+
private static bool? isHasThreadAccessPropertyPresent = null;
237+
238+
public static bool IsHasThreadAccessPropertyPresent
239+
{
240+
get
241+
{
242+
if (isHasThreadAccessPropertyPresent == null)
243+
{
244+
isHasThreadAccessPropertyPresent =
245+
Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent(typeof(Windows.System.DispatcherQueue).FullName, "HasThreadAccess");
246+
}
247+
return isHasThreadAccessPropertyPresent ?? false;
248+
}
249+
}
250+
236251
// https://www.travelneil.com/wndproc-in-uwp.html
237252
[ComImport, Guid("45D64A29-A63E-4CB6-B498-5781D298CB4F")]
238253
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]

Files/ViewModels/ItemViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ void ClearDisplay()
497497
UpdateEmptyTextType();
498498
DirectoryInfoUpdated?.Invoke(this, EventArgs.Empty);
499499
}
500-
if (CoreApplication.MainView.DispatcherQueue.HasThreadAccess)
500+
if (NativeWinApiHelper.IsHasThreadAccessPropertyPresent && CoreApplication.MainView.DispatcherQueue.HasThreadAccess)
501501
{
502502
ClearDisplay();
503503
}
@@ -585,7 +585,7 @@ void UpdateUI()
585585
DirectoryInfoUpdated?.Invoke(this, EventArgs.Empty);
586586
}
587587

588-
if (CoreApplication.MainView.DispatcherQueue.HasThreadAccess)
588+
if (NativeWinApiHelper.IsHasThreadAccessPropertyPresent && CoreApplication.MainView.DispatcherQueue.HasThreadAccess)
589589
{
590590
await Task.Run(ApplyChanges);
591591
UpdateUI();
@@ -620,7 +620,7 @@ void OrderEntries()
620620
filesAndFolders = SortingHelper.OrderFileList(filesAndFolders, folderSettings.DirectorySortOption, folderSettings.DirectorySortDirection).ToList();
621621
}
622622

623-
if (CoreApplication.MainView.DispatcherQueue.HasThreadAccess)
623+
if (NativeWinApiHelper.IsHasThreadAccessPropertyPresent && CoreApplication.MainView.DispatcherQueue.HasThreadAccess)
624624
{
625625
return Task.Run(OrderEntries);
626626
}

0 commit comments

Comments
 (0)