Skip to content

Commit

Permalink
Merge pull request #48968 from sharwell/require-token
Browse files Browse the repository at this point in the history
Update RegisterNotification to require a CancellationToken
  • Loading branch information
sharwell authored Oct 28, 2020
2 parents fbf1583 + 7660d18 commit b1d8852
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/EditorFeatures/Core/IForegroundNotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace Microsoft.CodeAnalysis.Editor
/// </summary>
internal interface IForegroundNotificationService
{
void RegisterNotification(Action action, IAsyncToken asyncToken, CancellationToken cancellationToken = default);
void RegisterNotification(Action action, IAsyncToken asyncToken, CancellationToken cancellationToken);

void RegisterNotification(Action action, int delayInMS, IAsyncToken asyncToken, CancellationToken cancellationToken = default);
void RegisterNotification(Action action, int delayInMS, IAsyncToken asyncToken, CancellationToken cancellationToken);

/// <summary>
/// if action return true, the service will call it back again when it has time.
/// </summary>
void RegisterNotification(Func<bool> action, IAsyncToken asyncToken, CancellationToken cancellationToken = default);
void RegisterNotification(Func<bool> action, IAsyncToken asyncToken, CancellationToken cancellationToken);

void RegisterNotification(Func<bool> action, int delayInMS, IAsyncToken asyncToken, CancellationToken cancellationToken = default);
void RegisterNotification(Func<bool> action, int delayInMS, IAsyncToken asyncToken, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public ForegroundNotificationService(IThreadingContext threadingContext)
}
}

public void RegisterNotification(Action action, IAsyncToken asyncToken, CancellationToken cancellationToken = default)
public void RegisterNotification(Action action, IAsyncToken asyncToken, CancellationToken cancellationToken)
=> RegisterNotification(action, DefaultTimeSliceInMS, asyncToken, cancellationToken);

public void RegisterNotification(Func<bool> action, IAsyncToken asyncToken, CancellationToken cancellationToken = default)
public void RegisterNotification(Func<bool> action, IAsyncToken asyncToken, CancellationToken cancellationToken)
=> RegisterNotification(action, DefaultTimeSliceInMS, asyncToken, cancellationToken);

public void RegisterNotification(Action action, int delay, IAsyncToken asyncToken, CancellationToken cancellationToken = default)
public void RegisterNotification(Action action, int delay, IAsyncToken asyncToken, CancellationToken cancellationToken)
{
Debug.Assert(delay >= 0);

Expand All @@ -73,7 +73,7 @@ public void RegisterNotification(Action action, int delay, IAsyncToken asyncToke
_workQueue.Enqueue(new PendingWork(current + delay, action, asyncToken, cancellationToken));
}

public void RegisterNotification(Func<bool> action, int delay, IAsyncToken asyncToken, CancellationToken cancellationToken = default)
public void RegisterNotification(Func<bool> action, int delay, IAsyncToken asyncToken, CancellationToken cancellationToken)
{
Debug.Assert(delay >= 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void ShowInfoBar(string message, params InfoBarUI[] items)
{
CreateInfoBar(infoBarHost, message, items);
}
}, _listener.BeginAsyncOperation(nameof(ShowInfoBar)));
}, _listener.BeginAsyncOperation(nameof(ShowInfoBar)), ThreadingContext.DisposalToken);
}

private bool TryGetInfoBarData(out IVsInfoBarHost infoBarHost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.Shell.Interop;
using Roslyn.Utilities;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem
{
Expand All @@ -19,6 +18,8 @@ private sealed class RuleSetFile : IRuleSetFile, IDisposable
{
private readonly VisualStudioRuleSetManager _ruleSetManager;
private readonly object _gate = new();
private readonly CancellationTokenSource _disposalCancellationSource;
private readonly CancellationToken _disposalToken;

private FileChangeWatcher.IContext _fileChangeContext;

Expand All @@ -34,6 +35,9 @@ public RuleSetFile(string filePath, VisualStudioRuleSetManager ruleSetManager)
{
FilePath = filePath;
_ruleSetManager = ruleSetManager;

_disposalCancellationSource = new();
_disposalToken = _disposalCancellationSource.Token;
}

public void InitializeFileTracking(FileChangeWatcher fileChangeWatcher)
Expand Down Expand Up @@ -140,7 +144,11 @@ private void EnsureDiagnosticOptionsRead()
}

public void Dispose()
=> RemoveFromRuleSetManagerAndDisconnectFileTrackers();
{
RemoveFromRuleSetManagerAndDisconnectFileTrackers();
_disposalCancellationSource.Cancel();
_disposalCancellationSource.Dispose();
}

private void RemoveFromRuleSetManagerAndDisconnectFileTrackers()
{
Expand Down Expand Up @@ -170,7 +178,7 @@ private void IncludeUpdated(object sender, string fileChanged)
// To avoid this, just queue up a Task to do the work on the foreground thread later, after
// the lock on the file change service has been released.
_ruleSetManager._foregroundNotificationService.RegisterNotification(
() => IncludeUpdateCore(), _ruleSetManager._listener.BeginAsyncOperation("IncludeUpdated"));
() => IncludeUpdateCore(), _ruleSetManager._listener.BeginAsyncOperation("IncludeUpdated"), _disposalToken);
}

private void IncludeUpdateCore()
Expand Down

0 comments on commit b1d8852

Please sign in to comment.