From 17a10b74b61921254e55e4b14af953c4af69705c Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 18 Oct 2008 14:35:16 +0000 Subject: [PATCH] Make FileChangeWatcher reload a file not only when there were external writes to the file, but also when the file was replaced by an external app. Fixes reloading the file when doing "svn revert". git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3604 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/Gui/WorkbenchSingleton.cs | 21 ++++++++++++++ .../Src/Services/File/FileChangeWatcher.cs | 28 +++++++++++-------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/Main/Base/Project/Src/Gui/WorkbenchSingleton.cs b/src/Main/Base/Project/Src/Gui/WorkbenchSingleton.cs index e273590a4b..2a6f0264e1 100644 --- a/src/Main/Base/Project/Src/Gui/WorkbenchSingleton.cs +++ b/src/Main/Base/Project/Src/Gui/WorkbenchSingleton.cs @@ -311,6 +311,27 @@ namespace ICSharpCode.SharpDevelop.Gui { caller.BeginCall(method, new object[] { arg1, arg2, arg3 }); } + + /// + /// Calls a method on the GUI thread, but delays the call a bit. + /// + public static void CallLater(int delayMilliseconds, Action method) + { + if (delayMilliseconds <= 0) + throw new ArgumentOutOfRangeException("delayMilliseconds", delayMilliseconds, "Value must be positive"); + if (method == null) + throw new ArgumentNullException("method"); + SafeThreadAsyncCall( + delegate { + Timer t = new Timer(); + t.Interval = delayMilliseconds; + t.Tick += delegate { + t.Stop(); + method(); + }; + t.Start(); + }); + } #endregion static void OnWorkbenchCreated() diff --git a/src/Main/Base/Project/Src/Services/File/FileChangeWatcher.cs b/src/Main/Base/Project/Src/Services/File/FileChangeWatcher.cs index e8ecd1ea45..66eac96cb0 100644 --- a/src/Main/Base/Project/Src/Services/File/FileChangeWatcher.cs +++ b/src/Main/Base/Project/Src/Services/File/FileChangeWatcher.cs @@ -80,8 +80,8 @@ namespace ICSharpCode.SharpDevelop public bool Enabled { get { return enabled; } - set { - enabled = value; + set { + enabled = value; SetWatcher(); } } @@ -111,11 +111,12 @@ namespace ICSharpCode.SharpDevelop if (watcher == null) { watcher = new FileSystemWatcher(); watcher.SynchronizingObject = WorkbenchSingleton.MainForm; - watcher.Changed += new FileSystemEventHandler(this.OnFileChangedEvent); + watcher.Changed += OnFileChangedEvent; + watcher.Created += OnFileChangedEvent; + watcher.Renamed += OnFileChangedEvent; } watcher.Path = Path.GetDirectoryName(fileName); watcher.Filter = Path.GetFileName(fileName); - watcher.NotifyFilter = NotifyFilters.LastWrite; watcher.EnableRaisingEvents = true; } catch (PlatformNotSupportedException) { if (watcher != null) { @@ -127,15 +128,18 @@ namespace ICSharpCode.SharpDevelop void OnFileChangedEvent(object sender, FileSystemEventArgs e) { - if (e.ChangeType != WatcherChangeTypes.Deleted) { - if (file == null) - return; - LoggingService.Debug("File " + file.FileName + " was changed externally"); + LoggingService.Debug("File " + file.FileName + " was changed externally: " + e.ChangeType); + if (file == null) + return; + if (!wasChangedExternally) { wasChangedExternally = true; if (WorkbenchSingleton.Workbench.IsActiveWindow) { - // delay showing message a bit, prevents showing two messages - // when the file changes twice in quick succession - WorkbenchSingleton.SafeThreadAsyncCall(MainForm_Activated, this, EventArgs.Empty); + // delay reloading message a bit, prevents showing two messages + // when the file changes twice in quick succession; and prevents + // trying to reload the file while it is still being written + WorkbenchSingleton.CallLater( + 500, + delegate { MainForm_Activated(this, EventArgs.Empty); } ); } } } @@ -149,6 +153,8 @@ namespace ICSharpCode.SharpDevelop return; string fileName = file.FileName; + if (!File.Exists(fileName)) + return; string message = StringParser.Parse("${res:ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.TextEditorDisplayBinding.FileAlteredMessage}", new string[,] {{"File", Path.GetFullPath(fileName)}}); if ((AutoLoadExternalChangesOption && file.IsDirty == false)