From 83168ce6bd129baa18a66865c1cbb836a666591b Mon Sep 17 00:00:00 2001 From: Nice3point Date: Tue, 21 May 2024 01:50:40 +0300 Subject: [PATCH 1/2] Upgrade dependencies --- source/RevitLookup/RevitLookup.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/RevitLookup/RevitLookup.csproj b/source/RevitLookup/RevitLookup.csproj index 77b1d29d..7039bfe9 100644 --- a/source/RevitLookup/RevitLookup.csproj +++ b/source/RevitLookup/RevitLookup.csproj @@ -44,7 +44,7 @@ - + @@ -76,7 +76,7 @@ - + From 2261f29119a460ccbe54c2549bddc2c26f68e5e5 Mon Sep 17 00:00:00 2001 From: Nice3point Date: Wed, 22 May 2024 13:08:22 +0300 Subject: [PATCH 2/2] Fix LookupService thread operations awaiting --- source/RevitLookup/Services/LookupService.cs | 88 ++++++++++---------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/source/RevitLookup/Services/LookupService.cs b/source/RevitLookup/Services/LookupService.cs index 48c4df67..f6b1f988 100644 --- a/source/RevitLookup/Services/LookupService.cs +++ b/source/RevitLookup/Services/LookupService.cs @@ -33,110 +33,110 @@ public sealed class LookupService : ILookupService { private static Dispatcher _dispatcher; private LookupServiceImpl _lookupService; - + static LookupService() { var uiThread = new Thread(Dispatcher.Run); uiThread.SetApartmentState(ApartmentState.STA); uiThread.Start(); - + EnsureThreadStart(uiThread); } - + public LookupService(IServiceScopeFactory scopeFactory) { - if (Thread.CurrentThread == _dispatcher.Thread) + if (_dispatcher.CheckAccess()) { _lookupService = new LookupServiceImpl(scopeFactory); } else { - _dispatcher.InvokeAsync(() => _lookupService = new LookupServiceImpl(scopeFactory)); + _dispatcher.InvokeAsync(() => _lookupService = new LookupServiceImpl(scopeFactory)).Wait(); } } - + public ILookupServiceDependsStage Snoop(SnoopableType snoopableType) { - if (Thread.CurrentThread == _dispatcher.Thread) + if (_dispatcher.CheckAccess()) { _lookupService.Snoop(snoopableType); } else { - _dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableType)); + _dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableType)).Wait(); } - + return this; } - + public ILookupServiceDependsStage Snoop(SnoopableObject snoopableObject) { - if (Thread.CurrentThread == _dispatcher.Thread) + if (_dispatcher.CheckAccess()) { _lookupService.Snoop(snoopableObject); } else { - _dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableObject)); + _dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableObject)).Wait(); } - + return this; } - + public ILookupServiceDependsStage Snoop(IList snoopableObjects) { - if (Thread.CurrentThread == _dispatcher.Thread) + if (_dispatcher.CheckAccess()) { _lookupService.Snoop(snoopableObjects); } else { - _dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableObjects)); + _dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableObjects)).Wait(); } - + return this; } - + public ILookupServiceShowStage DependsOn(IServiceProvider provider) { - if (Thread.CurrentThread == _dispatcher.Thread) + if (_dispatcher.CheckAccess()) { _lookupService.DependsOn(provider); } else { - _dispatcher.InvokeAsync(() => _lookupService.DependsOn(provider)); + _dispatcher.InvokeAsync(() => _lookupService.DependsOn(provider)).Wait(); } - + return this; } - + public ILookupServiceExecuteStage Show() where T : Page { - if (Thread.CurrentThread == _dispatcher.Thread) + if (_dispatcher.CheckAccess()) { _lookupService.Show(); } else { - _dispatcher.InvokeAsync(() => _lookupService.Show()); + _dispatcher.InvokeAsync(() => _lookupService.Show()).Wait(); } - + return this; } - + public void Execute(Action handler) where T : class { - if (Thread.CurrentThread == _dispatcher.Thread) + if (_dispatcher.CheckAccess()) { _lookupService.Execute(handler); } else { - _dispatcher.InvokeAsync(() => _lookupService.Execute(handler)); + _dispatcher.InvokeAsync(() => _lookupService.Execute(handler)).Wait(); } } - + private static void EnsureThreadStart(Thread thread) { Dispatcher dispatcher = null; @@ -146,11 +146,11 @@ private static void EnsureThreadStart(Thread thread) spinWait.SpinOnce(); dispatcher = Dispatcher.FromThread(thread); } - + _dispatcher = dispatcher; } - - private class LookupServiceImpl + + private sealed class LookupServiceImpl { private Window _owner; private Task _activeTask; @@ -158,38 +158,38 @@ private class LookupServiceImpl private readonly ISnoopVisualService _visualService; private readonly INavigationService _navigationService; private readonly Window _window; - + public LookupServiceImpl(IServiceScopeFactory scopeFactory) { _scope = scopeFactory.CreateScope(); - + _window = (Window) _scope.ServiceProvider.GetService(); _visualService = _scope.ServiceProvider.GetService(); _navigationService = _scope.ServiceProvider.GetService(); - + _window.Closed += (_, _) => _scope.Dispose(); } - + public void Snoop(SnoopableType snoopableType) { _activeTask = _visualService!.SnoopAsync(snoopableType); } - + public void Snoop(SnoopableObject snoopableObject) { _visualService.Snoop(snoopableObject); } - + public void Snoop(IList snoopableObjects) { _visualService.Snoop(snoopableObjects); } - + public void DependsOn(IServiceProvider provider) { _owner = (Window) provider.GetService(); } - + public void Show() where T : Page { if (_activeTask is null) @@ -201,7 +201,7 @@ public void Show() where T : Page _activeTask = _activeTask.ContinueWith(_ => ShowPage(), TaskScheduler.FromCurrentSynchronizationContext()); } } - + public void Execute(Action handler) where T : class { if (_activeTask is null) @@ -213,13 +213,13 @@ public void Execute(Action handler) where T : class _activeTask = _activeTask.ContinueWith(_ => InvokeHandler(handler), TaskScheduler.FromCurrentSynchronizationContext()); } } - + private void InvokeHandler(Action handler) where T : class { var service = _scope.ServiceProvider.GetService(); handler.Invoke(service); } - + private void ShowPage() where T : Page { if (_owner is null) @@ -231,7 +231,7 @@ private void ShowPage() where T : Page _window.Left = _owner.Left + 47; _window.Top = _owner.Top + 49; } - + _window.Show(Context.UiApplication.MainWindowHandle); _navigationService.Navigate(typeof(T)); }