Skip to content

Commit

Permalink
Fix some VS threading analyzer warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutch committed Apr 30, 2024
1 parent 047978b commit 7f8b9e1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ internal static class ComAggregate
/// will be forwarded to the managed implementation.
/// </summary>
internal static object CreateAggregatedObject (object managedObject)
=> WrapperPolicy.CreateAggregatedObject (managedObject);
{
Shell.ThreadHelper.ThrowIfNotOnUIThread ();
return WrapperPolicy.CreateAggregatedObject (managedObject);
}

/// <summary>
/// Return the RCW for the native IComWrapperFixed instance aggregating "managedObject"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal sealed class ComEventSink
{
public static ComEventSink Advise<T>(object obj, T sink) where T : class
{
Shell.ThreadHelper.ThrowIfNotOnUIThread ();

if (!typeof(T).IsInterface)
{
throw new InvalidOperationException();
Expand Down Expand Up @@ -46,6 +48,8 @@ public ComEventSink(IConnectionPoint connectionPoint, uint cookie)

public void Unadvise()
{
Shell.ThreadHelper.ThrowIfNotOnUIThread ();

if (_unadvised)
{
throw new InvalidOperationException("Already unadvised.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ internal static class WrapperPolicy
{
/// <summary>
/// Factory object for creating IComWrapperFixed instances.
/// Internal and not readonly so that unit tests can provide an alternative implementation.
/// </summary>
internal static IComWrapperFactory s_ComWrapperFactory =
(IComWrapperFactory)PackageUtilities.CreateInstance(typeof(IComWrapperFactory).GUID);
static IComWrapperFactory? s_ComWrapperFactory;

internal static object CreateAggregatedObject(object managedObject) => s_ComWrapperFactory.CreateAggregatedObject(managedObject);
internal static object CreateAggregatedObject(object managedObject)
{
ThreadHelper.ThrowIfNotOnUIThread();
s_ComWrapperFactory ??= (IComWrapperFactory)PackageUtilities.CreateInstance (typeof (IComWrapperFactory).GUID);
return s_ComWrapperFactory.CreateAggregatedObject(managedObject);
}

/// <summary>
/// Return the RCW for the native IComWrapperFixed instance aggregating "managedObject"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public int OnNewView (IVsTextView view)

public int RemoveAdornments ()
{
Shell.ThreadHelper.ThrowIfNotOnUIThread ();

_sink.Unadvise ();

return VSConstants.S_OK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public override IServiceProvider SystemServiceProvider
/// </summary>
internal void Setup()
{
Shell.ThreadHelper.ThrowIfNotOnUIThread ();

this.ComAggregate = CreateComAggregate();

// First, acquire any services we need throughout our lifetime.
Expand All @@ -84,7 +86,10 @@ internal void Setup()
}

private object CreateComAggregate()
=> Interop.ComAggregate.CreateAggregatedObject(this);
{
Shell.ThreadHelper.ThrowIfNotOnUIThread ();
return Interop.ComAggregate.CreateAggregatedObject(this);
}

internal void TearDown()
{
Expand Down

0 comments on commit 7f8b9e1

Please sign in to comment.