Skip to content

Commit

Permalink
Address feedbacks and restore some missing code from merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
bclothier committed Mar 15, 2018
1 parent b6cc24e commit 606f828
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
9 changes: 5 additions & 4 deletions Rubberduck.Main/Root/RubberduckIoCInstaller.cs
Expand Up @@ -38,16 +38,15 @@
using Rubberduck.UI.ToDoItems;
using Rubberduck.UI.UnitTesting;
using Rubberduck.UnitTesting;
using Rubberduck.VBEditor.Application;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using Rubberduck.VBEditor.SafeComWrappers.Office.Core.Abstract;
using Component = Castle.MicroKernel.Registration.Component;
using GeneralSettingsViewModel = Rubberduck.UI.Settings.GeneralSettingsViewModel;
using Rubberduck.UI.CodeMetrics;
using Rubberduck.VBEditor.ComManagement;
using Rubberduck.Parsing.Common;
using Rubberduck.VBEditor.ComManagement.TypeLibsAPI;
using Rubberduck.Inspections.Rubberduck.Inspections;
using Rubberduck.VBEditor.Events;
using Rubberduck.VBEditor.Utility;

namespace Rubberduck.Root
{
Expand Down Expand Up @@ -809,7 +808,9 @@ private void RegisterConstantVbeAndAddIn(IWindsorContainer container)
container.Register(Component.For<IVBE>().Instance(_vbe));
container.Register(Component.For<IAddIn>().Instance(_addin));
//note: This registration makes Castle Windsor inject _vbe_CommandBars in all ICommandBars Parent properties.
container.Register(Component.For<ICommandBars>().Instance(_vbe.CommandBars));
container.Register(Component.For<ICommandBars>().Instance(_vbe.CommandBars));
container.Register(Component.For<IUiContextProvider>().Instance(UiContextProvider.Instance()).LifestyleSingleton());
container.Register(Component.For<IVBEEvents>().Instance(VBEEvents.Initialize(_vbe)).LifestyleSingleton());
}

private static void RegisterHotkeyFactory(IWindsorContainer container)
Expand Down
9 changes: 7 additions & 2 deletions Rubberduck.Parsing/UIContext/UiDispatcher.cs
Expand Up @@ -7,6 +7,8 @@ namespace Rubberduck.Parsing.UIContext
{
public class UiDispatcher : IUiDispatcher
{
// thanks to Pellared on http://stackoverflow.com/a/12909070/1188513

private readonly IUiContextProvider _contextProvider;

public UiDispatcher(IUiContextProvider contextProvider)
Expand Down Expand Up @@ -92,12 +94,15 @@ public Task<T> StartTask<T>(Func<T> func, TaskCreationOptions options = TaskCrea
{
return StartTask(func, CancellationToken.None, options);
}


/// <remarks>
/// Depends on the static method: <see cref="UiContextProvider.Initialize"/>
/// </remarks>
private void CheckInitialization()
{
if (_contextProvider.UiContext == null)
{
throw new InvalidOperationException("UiDispatcher is not initialized. Invoke Initialize() from UI thread first.");
throw new InvalidOperationException("UiContext is not initialized. Invoke UiContextProvider.Initialize() from the UI thread first.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Rubberduck.VBEEditor/ComManagement/ComMessagePumper.cs
Expand Up @@ -44,7 +44,7 @@ public int PumpMessages()

private void CheckContext()
{
if (!_uiContext.CheckContext())
if (!_uiContext.IsExecutingInUiContext())
{
throw new InvalidOperationException("ComMessagePumper cannot be used in other threads. Only the UI thread can call methods on the ComMessagePumper");
}
Expand Down
2 changes: 1 addition & 1 deletion Rubberduck.VBEEditor/Rubberduck.VBEditor.csproj
Expand Up @@ -143,7 +143,7 @@
<Compile Include="ComManagement\ComSafeManager.cs" />
<Compile Include="ComManagement\IProjectsProvider.cs" />
<Compile Include="ComManagement\IProjectsRepository.cs" />
<Compile Include="Events\IVbeEvents.cs" />
<Compile Include="Events\IVBEEvents.cs" />
<Compile Include="ComManagement\ProjectsRepository.cs" />
<Compile Include="ComManagement\TypeLibs\TypeInfos.cs" />
<Compile Include="ComManagement\TypeLibs\TypeLibs.cs" />
Expand Down
14 changes: 7 additions & 7 deletions Rubberduck.VBEEditor/Utility/UiContext.cs
Expand Up @@ -6,15 +6,13 @@ namespace Rubberduck.VBEditor.Utility
{
public interface IUiContextProvider
{
bool CheckContext();
bool IsExecutingInUiContext();
SynchronizationContext UiContext { get; }
TaskScheduler UiTaskScheduler { get; }
}

public class UiContextProvider : IUiContextProvider
{
// thanks to Pellared on http://stackoverflow.com/a/12909070/1188513

private static SynchronizationContext Context { get; set; }
private static TaskScheduler TaskScheduler { get; set; }
private static readonly UiContextProvider UiContextInstance = new UiContextProvider();
Expand All @@ -26,11 +24,13 @@ public static void Initialize()
{
lock (Lock)
{
if (Context == null)
if (Context != null)
{
Context = SynchronizationContext.Current;
TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
return;
}

Context = SynchronizationContext.Current;
TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
}
}

Expand All @@ -39,7 +39,7 @@ public static void Initialize()
public SynchronizationContext UiContext => Context;
public TaskScheduler UiTaskScheduler => TaskScheduler;

public bool CheckContext()
public bool IsExecutingInUiContext()
{
lock (Lock)
{
Expand Down

0 comments on commit 606f828

Please sign in to comment.