Browse Source

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
shortcuts
Daniel Grunwald 17 years ago
parent
commit
17a10b74b6
  1. 21
      src/Main/Base/Project/Src/Gui/WorkbenchSingleton.cs
  2. 28
      src/Main/Base/Project/Src/Services/File/FileChangeWatcher.cs

21
src/Main/Base/Project/Src/Gui/WorkbenchSingleton.cs

@ -311,6 +311,27 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -311,6 +311,27 @@ namespace ICSharpCode.SharpDevelop.Gui
{
caller.BeginCall(method, new object[] { arg1, arg2, arg3 });
}
/// <summary>
/// Calls a method on the GUI thread, but delays the call a bit.
/// </summary>
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()

28
src/Main/Base/Project/Src/Services/File/FileChangeWatcher.cs

@ -80,8 +80,8 @@ namespace ICSharpCode.SharpDevelop @@ -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 @@ -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 @@ -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 @@ -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)

Loading…
Cancel
Save