public void StartFileChangeListeningAsync()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(FileChangeTracker));
            }

            Contract.ThrowIfTrue(_fileChangeCookie != s_none);

            _fileChangeCookie = new Lazy<uint?>(() =>
            {
                try
                {
                    Marshal.ThrowExceptionForHR(
                        _fileChangeService.AdviseFileChange(_filePath, FileChangeWatcher.FileChangeFlags, this, out var newCookie));
                    return newCookie;
                }
                catch (Exception e) when (ReportException(e))
                {
                    return null;
                }
            }, LazyThreadSafetyMode.ExecutionAndPublication);

            lock (s_lastBackgroundTaskGate)
            {
                s_lastBackgroundTask = s_lastBackgroundTask.ContinueWith(_ => _fileChangeCookie.Value, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default);
            }
        }
        public void StartListeningToFile(string file)
        {
            if (fileChangeEx == null || file == null)
            {
                return;
            }

            file = Path.GetFullPath(file);

            if (listenInfos.Any(i => file.Equals(i.Key, StringComparison.InvariantCultureIgnoreCase)))
            {
                return;
            }

            tracer.Trace("Start listening to file: {0}", this, file);

            uint cookie;

            ErrorHandler.ThrowOnFailure(
                fileChangeEx.AdviseFileChange(
                    file,
                    (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Time | _VSFILECHANGEFLAGS.VSFILECHG_Del),
                    this,
                    out cookie));
            listenInfos.Add(new KeyValuePair <string, uint>(file, cookie));
        }
        private void RegisterProject(IReloadableProject project)
        {
            uint filechangeCookie;

            lock (_registeredProjects)
            {
                _registeredProjects.TryGetValue(project, out filechangeCookie);
            }

            System.Diagnostics.Debug.Assert(filechangeCookie == VSConstants.VSCOOKIE_NIL);
            if (filechangeCookie == VSConstants.VSCOOKIE_NIL)
            {
                IVsFileChangeEx fileChangeService = _serviceProvider.GetService <IVsFileChangeEx, SVsFileChangeEx>();
                if (fileChangeService != null)
                {
                    int hr = fileChangeService.AdviseFileChange(project.ProjectFile, (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Time | _VSFILECHANGEFLAGS.VSFILECHG_Size), this, out filechangeCookie);
                    System.Diagnostics.Debug.Assert(ErrorHandler.Succeeded(hr) && filechangeCookie != VSConstants.VSCOOKIE_NIL);
                    if (ErrorHandler.Succeeded(hr) && filechangeCookie != VSConstants.VSCOOKIE_NIL)
                    {
                        lock (_registeredProjects)
                        {
                            _registeredProjects.Add(project, filechangeCookie);
                        }
                    }
                    else
                    {
                        throw new COMException(string.Format(Resources.FailedToWatchProject, project.ProjectFile), hr);
                    }
                }
            }
        }
Exemple #4
0
 private void RegisterFileWatcherAsync(string projectLockJsonFilePath)
 {
     if (_fileChangeService != null)
     {
         int hr = _fileChangeService.AdviseFileChange(projectLockJsonFilePath, (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Time | _VSFILECHANGEFLAGS.VSFILECHG_Size | _VSFILECHANGEFLAGS.VSFILECHG_Add | _VSFILECHANGEFLAGS.VSFILECHG_Del), this, out _filechangeCookie);
         ErrorHandler.ThrowOnFailure(hr);
     }
 }
Exemple #5
0
            public DiffToolMonitor(IAnkhServiceProvider context, string monitor, bool monitorDir, int[] resolvedExitCodes)
                : base(context)
            {
                ThreadHelper.ThrowIfNotOnUIThread();

                if (string.IsNullOrEmpty(monitor))
                {
                    throw new ArgumentNullException("monitor");
                }
                else if (!SvnItem.IsValidPath(monitor))
                {
                    throw new ArgumentOutOfRangeException("monitor");
                }

                _monitorDir = monitorDir;
                _toMonitor  = monitor;

                IVsFileChangeEx fx = GetService <IVsFileChangeEx>(typeof(SVsFileChangeEx));

                _cookie = 0;
                if (fx == null)
                {
                }
                else if (!_monitorDir)
                {
                    if (!VSErr.Succeeded(fx.AdviseFileChange(monitor,
                                                             (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Time | _VSFILECHANGEFLAGS.VSFILECHG_Size
                                                                    | _VSFILECHANGEFLAGS.VSFILECHG_Add | _VSFILECHANGEFLAGS.VSFILECHG_Del
                                                                    | _VSFILECHANGEFLAGS.VSFILECHG_Attr),
                                                             this,
                                                             out _cookie)))
                    {
                        _cookie = 0;
                    }
                }
                else
                {
                    if (!VSErr.Succeeded(fx.AdviseDirChange(monitor, 1, this, out _cookie)))
                    {
                        _cookie = 0;
                    }
                }

                IAnkhOpenDocumentTracker odt = GetService <IAnkhOpenDocumentTracker>();

                if (odt != null)
                {
                    if (odt.IgnoreChanges(_toMonitor, true))
                    {
                        _odt = odt;
                    }
                }

                if (resolvedExitCodes != null)
                {
                    _resolvedExitCodes = (int[])resolvedExitCodes.Clone();
                }
            }
Exemple #6
0
        private void RegisterFileWatcher(string projectLockJsonFilePath)
        {
            IVsFileChangeEx fileChangeService = _serviceProvider.GetService <IVsFileChangeEx, SVsFileChangeEx>();

            if (fileChangeService != null)
            {
                int hr = fileChangeService.AdviseFileChange(projectLockJsonFilePath, (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Time | _VSFILECHANGEFLAGS.VSFILECHG_Size | _VSFILECHANGEFLAGS.VSFILECHG_Add | _VSFILECHANGEFLAGS.VSFILECHG_Del), this, out _filechangeCookie);
                ErrorHandler.ThrowOnFailure(hr);
            }
        }
        public ConfigurationFileTracker(string configurationFilename, IVsFileChangeEx fileChangeService, Action onConfigurationFileChanged)
        {
            this.fileChangeService          = fileChangeService;
            this.onConfigurationFileChanged = onConfigurationFileChanged;

            fileChangeService.AdviseFileChange(
                configurationFilename,
                (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Size | _VSFILECHANGEFLAGS.VSFILECHG_Time),
                this,
                out fileChangeCookie);
        }
        public void StartFileChangeListeningAsync()
        {
            watchedFileId = new Lazy <uint>(() =>
            {
                uint newCookie;
                Marshal.ThrowExceptionForHR(
                    fileChangeService.AdviseFileChange(filePath, (uint)_VSFILECHANGEFLAGS.VSFILECHG_Time, this, out newCookie));
                return(newCookie);
            }, LazyThreadSafetyMode.ExecutionAndPublication);

            Task.Run(() => watchedFileId.Value, CancellationToken.None);
        }
Exemple #9
0
        private void RegisterFileWatcher(string projectLockJsonFilePath)
        {
            // Note file change service is free-threaded
            if (_fileChangeService != null && projectLockJsonFilePath != null)
            {
                _previousContentsHash = GetFileHashOrNull(projectLockJsonFilePath);
                _taskDelayScheduler   = new TaskDelayScheduler(
                    s_notifyDelay,
                    _projectServices.ThreadingService,
                    CreateLinkedCancellationToken());

                int hr = _fileChangeService.AdviseFileChange(projectLockJsonFilePath, (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Time | _VSFILECHANGEFLAGS.VSFILECHG_Size | _VSFILECHANGEFLAGS.VSFILECHG_Add | _VSFILECHANGEFLAGS.VSFILECHG_Del), this, out _filechangeCookie);
                ErrorHandler.ThrowOnFailure(hr);
            }

            _fileBeingWatched = projectLockJsonFilePath;
        }
Exemple #10
0
        /// <summary>
        /// In this function we inform the shell when we wish to receive
        /// events when our file is changed or we inform the shell when
        /// we wish not to receive events anymore.
        /// </summary>
        /// <param name="pszFileName">File name string</param>
        /// <param name="fStart">TRUE indicates advise, FALSE indicates unadvise.</param>
        /// <returns>Result of the operation</returns>
        private int SetFileChangeNotification(string pszFileName, bool fStart)
        {
            Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "\t **** Inside SetFileChangeNotification ****"));

            int result = VSConstants.E_FAIL;

            //Get the File Change service
            if (null == vsFileChangeEx)
            {
                vsFileChangeEx = (IVsFileChangeEx)GetService(typeof(SVsFileChangeEx));
            }
            if (null == vsFileChangeEx)
            {
                return(VSConstants.E_UNEXPECTED);
            }

            // Setup Notification if fStart is TRUE, Remove if fStart is FALSE.
            if (fStart)
            {
                if (vsFileChangeCookie == VSConstants.VSCOOKIE_NIL)
                {
                    //Receive notifications if either the attributes of the file change or
                    //if the size of the file changes or if the last modified time of the file changes
                    result = vsFileChangeEx.AdviseFileChange(pszFileName,
                                                             (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Attr | _VSFILECHANGEFLAGS.VSFILECHG_Size | _VSFILECHANGEFLAGS.VSFILECHG_Time),
                                                             (IVsFileChangeEvents)this,
                                                             out vsFileChangeCookie);
                    if (vsFileChangeCookie == VSConstants.VSCOOKIE_NIL)
                    {
                        return(VSConstants.E_FAIL);
                    }
                }
            }
            else
            {
                if (vsFileChangeCookie != VSConstants.VSCOOKIE_NIL)
                {
                    result             = vsFileChangeEx.UnadviseFileChange(vsFileChangeCookie);
                    vsFileChangeCookie = VSConstants.VSCOOKIE_NIL;
                }
            }
            return(result);
        }
        public void StartFileChangeListeningAsync()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(typeof(FileChangeTracker).Name);
            }

            Contract.ThrowIfTrue(_fileChangeCookie != s_none);

            _fileChangeCookie = new Lazy <uint>(() =>
            {
                Marshal.ThrowExceptionForHR(
                    _fileChangeService.AdviseFileChange(_filePath, FileChangeFlags, this, out var newCookie));
                return(newCookie);
            }, LazyThreadSafetyMode.ExecutionAndPublication);

            // file change service is free-threaded. start running it in background right away
            Task.Run(() => _fileChangeCookie.Value, CancellationToken.None);
        }
        private void HookFileChanges(bool reHook)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            IVsFileChangeEx fileChange = null;

            if (_fileChangeCookies != null)
            {
                fileChange = GetService <IVsFileChangeEx>(typeof(SVsFileChangeEx));
                uint[] list = _fileChangeCookies;
                _fileChangeCookies = null;

                foreach (uint u in list)
                {
                    if (u != 0)
                    {
                        fileChange.UnadviseFileChange(u);
                    }
                }
            }

            if (reHook && _isFileDocument)
            {
                if (fileChange == null)
                {
                    fileChange = GetService <IVsFileChangeEx>(typeof(SVsFileChangeEx));
                }

                List <string> items = new List <string>(GetService <IProjectFileMapper>().GetAllDocumentFiles(_name));

                uint[] cookies = new uint[items.Count];
                _fileChangeCookies = cookies;

                for (int i = 0; i < items.Count; i++)
                {
                    uint ck;
                    if (VSErr.Succeeded(fileChange.AdviseFileChange(items[i], (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Size | _VSFILECHANGEFLAGS.VSFILECHG_Time), this, out ck)))
                    {
                        cookies[i] = ck;
                    }
                }
            }
        }
Exemple #13
0
        /// <summary>
        /// Sets the file change notification - whether VS should be informed about outside-VS file changes
        /// </summary>
        /// <param name="fileNameToNotify">File path</param>
        /// <param name="startNotify">True to turn notifications on</param>
        private int SetFileChangeNotification(string fileNameToNotify, bool startNotify)
        {
            int result = VSConstants.E_FAIL;

            IVsFileChangeEx vsFileChangeEx = (IVsFileChangeEx)Package.GetGlobalService(typeof(SVsFileChangeEx));

            if (null == vsFileChangeEx)
            {
                return(VSConstants.E_UNEXPECTED);
            }

            // Setup Notification if startNotify is TRUE, Remove if startNotify is FALSE.
            if (startNotify)
            {
                if (vsFileChangeCookie == VSConstants.VSCOOKIE_NIL)
                {
                    //Receive notifications if either the attributes of the file change or
                    //if the size of the file changes or if the last modified time of the file changes
                    result = vsFileChangeEx.AdviseFileChange(fileNameToNotify,
                                                             (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Attr | _VSFILECHANGEFLAGS.VSFILECHG_Size | _VSFILECHANGEFLAGS.VSFILECHG_Time),
                                                             (IVsFileChangeEvents)this,
                                                             out vsFileChangeCookie);
                    if (vsFileChangeCookie == VSConstants.VSCOOKIE_NIL)
                    {
                        return(VSConstants.E_FAIL);
                    }
                }
                result = VSConstants.S_OK;
            }
            else
            {
                if (vsFileChangeCookie != VSConstants.VSCOOKIE_NIL)
                {
                    //if we want to unadvise and the cookieTextViewEvents isnt null then unadvise changes
                    result             = vsFileChangeEx.UnadviseFileChange(vsFileChangeCookie);
                    vsFileChangeCookie = VSConstants.VSCOOKIE_NIL;
                    result             = VSConstants.S_OK;
                }
            }
            return(result);
        }
        public void Subscribe(string filepath, bool prelowered)
        {
            if (fileChangeEx != null && !String.IsNullOrEmpty(filepath))
            {
                if (!prelowered)
                {
                    filepath = filepath.ToLower();
                }

                lock (eventCookies)
                {
                    if (!eventCookies.ContainsKey(filepath))
                    {
                        uint cookie;
                        ErrorHandler.ThrowOnFailure(fileChangeEx.AdviseFileChange(filepath, (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Size | _VSFILECHANGEFLAGS.VSFILECHG_Time), this, out cookie));
                        MyPackage.OutputGeneral("Tracking \"" + filepath + "\"");
                        eventCookies.Add(filepath, cookie);
                    }
                }
            }
        }
            public override IDisposable MonitorChangesForReload()
            {
                if (_monitor.Count > 0)
                {
                    return(null);
                }

                foreach (string path in _ignoring)
                {
                    uint cky;

                    // BH: We don't monitor the attributes as some SVN actions put files temporary on read only!
                    if (VSErr.Succeeded(_change.AdviseFileChange(path, (uint)(/*_VSFILECHANGEFLAGS.VSFILECHG_Attr |*/
                                                                     _VSFILECHANGEFLAGS.VSFILECHG_Size | _VSFILECHANGEFLAGS.VSFILECHG_Time), this, out cky)))
                    {
                        _monitor.Add(cky, path);
                    }
                }

                return(new ReloadModifiedDisposer(this));
            }
Exemple #16
0
        public override void StartListening()
        {
            _foregroundDispatcher.AssertForegroundThread();

            try
            {
                if (_fileChangeCookie == VSConstants.VSCOOKIE_NIL)
                {
                    var hr = _fileChangeService.AdviseFileChange(
                        FilePath,
                        FileChangeFlags,
                        this,
                        out _fileChangeCookie);

                    Marshal.ThrowExceptionForHR(hr);
                }
            }
            catch (Exception exception)
            {
                _errorReporter.ReportError(exception);
            }
        }
Exemple #17
0
        public Task StartFileChangeListeningAsync()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(FileChangeTracker));
            }

            Contract.ThrowIfTrue(_fileChangeCookie != s_none);

            _fileChangeCookie = new AsyncLazy <uint?>(async cancellationToken =>
            {
                try
                {
                    return(await((IVsAsyncFileChangeEx)_fileChangeService).AdviseFileChangeAsync(_filePath, _fileChangeFlags, this).ConfigureAwait(false));
                }
                catch (Exception e) when(ReportException(e))
                {
                    return(null);
                }
            }, cancellationToken =>
            {
                try
                {
                    Marshal.ThrowExceptionForHR(
                        _fileChangeService.AdviseFileChange(_filePath, (uint)_fileChangeFlags, this, out var newCookie));
                    return(newCookie);
                }
                catch (Exception e) when(ReportException(e))
                {
                    return(null);
                }
            }, cacheResult: true);

            lock (s_lastBackgroundTaskGate)
            {
                s_lastBackgroundTask = s_lastBackgroundTask.ContinueWith(_ => _fileChangeCookie.GetValueAsync(CancellationToken.None), CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default).Unwrap();
                return(s_lastBackgroundTask);
            }
        }
 /// <summary>
 ///     Starts this instance.
 /// </summary>
 public void Start()
 {
     _fileChangeService.AdviseFileChange(_configFilePath, (uint)(_VSFILECHANGEFLAGS.VSFILECHG_Size | _VSFILECHANGEFLAGS.VSFILECHG_Time), this, out _fileChangeCookie);
     _isRunning = true;
 }