Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions FileMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;

using System.Threading;

namespace MEditor
{

public class FileMonitor
{
private int TimeoutMillis = 2000;
private readonly int TimeoutMillis = 2000;

System.IO.FileSystemWatcher fsw = new System.IO.FileSystemWatcher();
System.Threading.Timer m_timer = null;
List<String> files = new List<string>();
FileSystemEventHandler fswHandler = null;
private readonly List<String> files = new List<string>();
private readonly FileSystemEventHandler fswHandler;
private readonly Timer m_timer;
private FileSystemWatcher fsw = new FileSystemWatcher();

public FileMonitor(FileSystemEventHandler watchHandler)
{
m_timer = new System.Threading.Timer(new TimerCallback(OnTimer), null, Timeout.Infinite, Timeout.Infinite);
m_timer = new Timer(OnTimer, null, Timeout.Infinite, Timeout.Infinite);
fswHandler = watchHandler;

}


public FileMonitor(FileSystemEventHandler watchHandler, int timerInterval)
{
m_timer = new System.Threading.Timer(new TimerCallback(OnTimer), null, Timeout.Infinite, Timeout.Infinite);
m_timer = new Timer(OnTimer, null, Timeout.Infinite, Timeout.Infinite);
TimeoutMillis = timerInterval;
fswHandler = watchHandler;

}

public void Add(string dir,string filter)
public void Add(string dir, string filter)
{
System.IO.FileSystemWatcher fsw = new System.IO.FileSystemWatcher(dir, filter);
fsw.NotifyFilter = System.IO.NotifyFilters.LastWrite;
fsw.Changed += new FileSystemEventHandler(OnFileChanged);
var fsw = new FileSystemWatcher(dir, filter);
fsw.NotifyFilter = NotifyFilters.LastWrite;
fsw.Changed += OnFileChanged;
fsw.EnableRaisingEvents = true;
}

public void OnFileChanged(object sender, FileSystemEventArgs e)
{
//MessageBox.Show("Created", "Create triggered");
Mutex mutex = new Mutex(false, "FSW");
var mutex = new Mutex(false, "FSW");
mutex.WaitOne();
if (!files.Contains(e.FullPath))
{
Expand All @@ -57,9 +52,9 @@ public void OnFileChanged(object sender, FileSystemEventArgs e)

private void OnTimer(object state)
{
List<String> backup = new List<string>();
var backup = new List<string>();

Mutex mutex = new Mutex(false, "FSW");
var mutex = new Mutex(false, "FSW");
mutex.WaitOne();
backup.AddRange(files);
files.Clear();
Expand All @@ -70,7 +65,6 @@ private void OnTimer(object state)
{
fswHandler(this, new FileSystemEventArgs(WatcherChangeTypes.Changed, string.Empty, file));
}

}
}
}
}
Loading