Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
    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));
    }
Ejemplo n.º 3
0
 // 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);
     }
 }
Ejemplo n.º 5
0
 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);
     }
 }
Ejemplo n.º 6
0
 private void StartCleanupTimer()
 {
     lock (_cleanupTimerLock)
     {
         if (!_cleanupTimer.Available)
         {
             _cleanupTimer = NonCapturingTimer.Create(_cleanupCallback, this, DefaultCleanupInterval, Timeout.InfiniteTimeSpan);
         }
     }
 }
Ejemplo n.º 7
0
        // 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);
                }
            }
        }
Ejemplo n.º 8
0
    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);
    }
Ejemplo n.º 9
0
 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);
     }
 }
Ejemplo n.º 10
0
        // 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;
     }
 }
Ejemplo n.º 12
0
        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);
            }
        }
Ejemplo n.º 13
0
        /// <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);
        }
Ejemplo n.º 14
0
        /// <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);
        }
Ejemplo n.º 15
0
 public AckHandler()
 {
     _timer = NonCapturingTimer.Create(state => ((AckHandler)state).CheckAcks(), state: this, dueTime: _ackInterval, period: _ackInterval);
 }