/// <summary> /// Initializes an instance of <see cref="PhysicalFilesWatcher" /> that watches files in <paramref name="root" />. /// Wraps an instance of <see cref="System.IO.FileSystemWatcher" /> /// </summary> /// <param name="root">Root directory for the watcher</param> /// <param name="fileSystemWatcher">The wrapped watcher that is watching <paramref name="root" /></param> /// <param name="pollForChanges"> /// True when the watcher should use polling to trigger instances of /// <see cref="IChangeToken" /> created by <see cref="CreateFileChangeToken(string)" /> /// </param> /// <param name="filters">Specifies which files or directories are excluded. Notifications of changes to are not raised to these.</param> public PhysicalFilesWatcher( string root, FileSystemWatcher fileSystemWatcher, bool pollForChanges, ExclusionFilters filters) { if (fileSystemWatcher == null && !pollForChanges) { throw new ArgumentNullException(nameof(fileSystemWatcher), SR.Error_FileSystemWatcherRequiredWithoutPolling); } _root = root; if (fileSystemWatcher != null) { _fileWatcher = fileSystemWatcher; _fileWatcher.IncludeSubdirectories = true; _fileWatcher.Created += OnChanged; _fileWatcher.Changed += OnChanged; _fileWatcher.Renamed += OnRenamed; _fileWatcher.Deleted += OnChanged; _fileWatcher.Error += OnError; } PollForChanges = pollForChanges; _filters = filters; PollingChangeTokens = new ConcurrentDictionary <IPollingChangeToken, IPollingChangeToken>(); _timerFactory = () => NonCapturingTimer.Create(RaiseChangeEvents, state: PollingChangeTokens, dueTime: TimeSpan.Zero, period: DefaultPollingInterval); }
public async Task NonCapturingTimer_DoesntCaptureExecutionContext() { // Arrange var message = new AsyncLocal <string>(); message.Value = "Hey, this is a value stored in the execuion context"; var tcs = new TaskCompletionSource <string>(); // Act var timer = NonCapturingTimer.Create((_) => { // Observe the value based on the current execution context tcs.SetResult(message.Value); }, state: null, dueTime: TimeSpan.FromMilliseconds(1), Timeout.InfiniteTimeSpan); // Assert var messageFromTimer = await tcs.Task; timer.Dispose(); // ExecutionContext didn't flow to timer callback Assert.Null(messageFromTimer); // ExecutionContext was restored Assert.NotNull(await Task.Run(() => message.Value)); }
// Internal so it can be overridden in tests internal virtual void StartCleanupTimer() { lock (_cleanupTimerLock) { _cleanupTimer ??= NonCapturingTimer.Create(_cleanupCallback, this, DefaultCleanupInterval, Timeout.InfiniteTimeSpan); } }
private void StartWorker() { // Access to the timer is protected by the lock in Synchronize and in Timer_Tick if (_timer == null) { // Timer will fire after a fixed delay, but only once. _timer = NonCapturingTimer.Create(Timer_Tick, null, Delay, Timeout.InfiniteTimeSpan); } }
protected virtual void StartWorker() { // Access to the timer is protected by the lock in Enqueue and in Timer_Tick if (_timer == null) { // Timer will fire after a fixed delay, but only once. _timer = NonCapturingTimer.Create(state => ((BackgroundDocumentGenerator)state).Timer_Tick(), this, Delay, Timeout.InfiniteTimeSpan); } }
private void StartCleanupTimer() { lock (_cleanupTimerLock) { if (!_cleanupTimer.Available) { _cleanupTimer = NonCapturingTimer.Create(_cleanupCallback, this, DefaultCleanupInterval, Timeout.InfiniteTimeSpan); } } }
// Internal for testing internal void StartIdleTimer() { _dispatcher.AssertForegroundThread(); lock (IdleLock) { if (_idleTimer == null) { // Timer will fire after a fixed delay, but only once. _idleTimer = NonCapturingTimer.Create(state => ((DefaultVisualStudioRazorParser)state).Timer_Tick(), this, IdleDelay, Timeout.InfiniteTimeSpan); } } }
public Task StartAsync(CancellationToken cancellationToken = default) { if (_publishers.Length == 0) { return(Task.CompletedTask); } // IMPORTANT - make sure this is the last thing that happens in this method. The timer can // fire before other code runs. _timer = NonCapturingTimer.Create(Timer_Tick, null, dueTime: _options.Value.Delay, period: _options.Value.Period); return(Task.CompletedTask); }
private void StartWorker() { // Access to the timer is protected by the lock in Enqueue and in Timer_TickAsync if (_timer == null) { // Timer will fire after a fixed delay, but only once. _timer = NonCapturingTimer.Create( state => _ = ((BatchingWorkQueue)state).Timer_TickAsync(), this, _batchingTimeSpan, Timeout.InfiniteTimeSpan); } }
// Internal for testing internal void StartIdleTimer() { _joinableTaskContext.AssertUIThread(); lock (_idleLock) { if (_idleTimer == null) { // Timer will fire after a fixed delay, but only once. _idleTimer = NonCapturingTimer.Create(state => ((DefaultVisualStudioRazorParser)state).Timer_Tick(), this, _idleDelay, Timeout.InfiniteTimeSpan); } } }
private void StartExpiryTimerSlow(TimerCallback callback) { lock (_lock) { if (Volatile.Read(ref _timerInitialized)) { return; } _callback = callback; _timer = NonCapturingTimer.Create(TimerCallback, this, Lifetime, Timeout.InfiniteTimeSpan); _timerInitialized = true; } }
private void StartExpiryTimerSlow(TimerCallback <ActiveHandlerTrackingEntry> callback) { Debug.Assert(Lifetime != Timeout.InfiniteTimeSpan); lock (_lock) { if (Volatile.Read(ref _timerInitialized)) { return; } _callback = callback; _timer = NonCapturingTimer.Create(_timerCallback, this, Lifetime, Timeout.InfiniteTimeSpan); Volatile.Write(ref _timerInitialized, true); } }
/// <summary> /// Initializes an instance of <see cref="PhysicalFilesWatcher" /> that watches files in <paramref name="root" />. /// Wraps an instance of <see cref="System.IO.FileSystemWatcher" /> /// </summary> /// <param name="root">Root directory for the watcher</param> /// <param name="fileSystemWatcher">The wrapped watcher that is watching <paramref name="root" /></param> /// <param name="pollForChanges"> /// True when the watcher should use polling to trigger instances of /// <see cref="IChangeToken" /> created by <see cref="CreateFileChangeToken(string)" /> /// </param> /// <param name="filters">Specifies which files or directories are excluded. Notifications of changes to are not raised to these.</param> public PhysicalFilesWatcher( string root, FileSystemWatcher fileSystemWatcher, bool pollForChanges, ExclusionFilters filters) { _root = root; _fileWatcher = fileSystemWatcher; _fileWatcher.IncludeSubdirectories = true; _fileWatcher.Created += OnChanged; _fileWatcher.Changed += OnChanged; _fileWatcher.Renamed += OnRenamed; _fileWatcher.Deleted += OnChanged; _fileWatcher.Error += OnError; PollForChanges = pollForChanges; _filters = filters; PollingChangeTokens = new ConcurrentDictionary <IPollingChangeToken, IPollingChangeToken>(); _timerFactory = () => NonCapturingTimer.Create(RaiseChangeEvents, state: PollingChangeTokens, dueTime: TimeSpan.Zero, period: DefaultPollingInterval); }
/// <summary> /// Initializes an instance of <see cref="PhysicalFilesWatcher" /> that watches files in <paramref name="root" />. /// Wraps an instance of <see cref="System.IO.FileSystemWatcher" /> /// </summary> /// <param name="root">Root directory for the watcher</param> /// <param name="fileSystemWatcher">The wrapped watcher that is watching <paramref name="root" /></param> /// <param name="pollForChanges"> /// True when the watcher should use polling to trigger instances of /// <see cref="IChangeToken" /> created by <see cref="CreateFileChangeToken(string)" /> /// </param> /// <param name="filters">Specifies which files or directories are excluded. Notifications of changes to are not raised to these.</param> public PhysicalFilesWatcher( string root, FileSystemWatcher fileSystemWatcher, bool pollForChanges, ExclusionFilters filters) { if (fileSystemWatcher == null && !pollForChanges) { throw new ArgumentNullException(nameof(fileSystemWatcher), SR.Error_FileSystemWatcherRequiredWithoutPolling); } _root = root; if (fileSystemWatcher != null) { #if NETCOREAPP if (OperatingSystem.IsBrowser() || (OperatingSystem.IsIOS() && !OperatingSystem.IsMacCatalyst()) || OperatingSystem.IsTvOS()) { throw new PlatformNotSupportedException(SR.Format(SR.FileSystemWatcher_PlatformNotSupported, typeof(FileSystemWatcher))); } #endif _fileWatcher = fileSystemWatcher; _fileWatcher.IncludeSubdirectories = true; _fileWatcher.Created += OnChanged; _fileWatcher.Changed += OnChanged; _fileWatcher.Renamed += OnRenamed; _fileWatcher.Deleted += OnChanged; _fileWatcher.Error += OnError; } PollForChanges = pollForChanges; _filters = filters; PollingChangeTokens = new ConcurrentDictionary <IPollingChangeToken, IPollingChangeToken>(); _timerFactory = () => NonCapturingTimer.Create(RaiseChangeEvents, state: PollingChangeTokens, dueTime: TimeSpan.Zero, period: DefaultPollingInterval); }
public AckHandler() { _timer = NonCapturingTimer.Create(state => ((AckHandler)state).CheckAcks(), state: this, dueTime: _ackInterval, period: _ackInterval); }