public static FileChecker TryCreate(ReadedFileInfo fileInfo, WatchMode watch, TimeSpan?delay, CheckMode check) { switch (watch) { case WatchMode.None: return(null); case WatchMode.Time: if (delay == null) { throw new ArgumentNullException("delay"); } return(new PeriodicFileChecker(fileInfo, delay.Value, check)); case WatchMode.Auto: try { return(new WatchedFileChecker(fileInfo, delay, check)); } catch (Exception) { if (delay == null) { throw; } } return(new PeriodicFileChecker(fileInfo, delay.Value, check)); case WatchMode.System: return(new WatchedFileChecker(fileInfo, delay, check)); default: throw new ArgumentOutOfRangeException("unexpected mode"); } }
public static FileChecker TryCreate(ReadedFileInfo fileInfo, WatchMode watch, TimeSpan? delay, CheckMode check) { switch (watch) { case WatchMode.None: return null; case WatchMode.Time: if (delay == null) throw new ArgumentNullException("delay"); return new PeriodicFileChecker(fileInfo, delay.Value, check); case WatchMode.Auto: try { return new WatchedFileChecker(fileInfo, delay, check); } catch (Exception) { if (delay == null) throw; } return new PeriodicFileChecker(fileInfo, delay.Value, check); case WatchMode.System: return new WatchedFileChecker(fileInfo, delay, check); default: throw new ArgumentOutOfRangeException("unexpected mode"); } }
public FileMonitor(string fileName, byte[] expectedContent, WatchMode mode, TimeSpan? delay) { _fileName = fileName; _content = expectedContent; lock (_sync) { if (mode == WatchMode.Time) { if (delay == null) throw new ArgumentNullException("delay time is null"); SetTimer(delay.Value); } else if (mode == WatchMode.Auto) { _watcher = TryCreateWatch(!delay.HasValue); if (_watcher == null) SetTimer(delay.Value); } else if (mode == WatchMode.System) { _watcher = TryCreateWatch(true); } else throw new ArgumentOutOfRangeException("unexpected mode"); } AsyncReview(); }
public void WrongArguments(WatchMode mode, int? ms) { TimeSpan? ts = null; if (ms != null) ts = TimeSpan.FromMilliseconds(ms.Value); string file = Path.GetTempFileName(); try { new FileMonitor(file, new byte[] { 1, 2, 3 }, mode, ts); } catch (ArgumentException) { } }
public void FileChange(WatchMode mode, int?ms) { TimeSpan?ts = null; if (ms != null) { ts = TimeSpan.FromMilliseconds(ms.Value); } string file = Path.GetTempFileName(); File.WriteAllBytes(file, new byte[] { 1, 2, 3 }); var fileInfo = ReadedFileInfo.Create(file, _ => _.CopyTo(Stream.Null)); var m = FileChecker.TryCreate(fileInfo, mode, ts, CheckMode.All); var wait = new ManualResetEvent(false); m.Changed += (a, e) => { wait.Set(); }; var t = Task.Factory.StartNew(() => { Thread.Sleep(1000); using (var fs = new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { fs.Position = 2; fs.WriteByte(1); fs.Close(); }; }, TaskCreationOptions.LongRunning); Task.WaitAll(t); Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed"); wait.Reset(); m.Changed += (a, e) => { wait.Set(); }; Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed"); }
/// <summary> /// Constructor /// </summary> /// <param name="memoryAccess"></param> public RAMWatch(ref MemoryAccess memoryAccess, WatchMode watchMode, int dispatchTimeout = 1000) { _memoryReader = memoryAccess.MemoryReader; _memoryWriter = memoryAccess.MemoryWriter; _threads = new Dictionary <string, Thread>(); _watchMode = watchMode; RAMValueQueue = new ConcurrentQueue <RAMValue>(); switch (_watchMode) { // Single Thread case WatchMode.SingleThreaded: WatchList = new List <RAMValue>(); _threads.Add("rwf_0", new Thread(new ThreadStart(delegate() { SingleThreadWatch(); }))); _threads["rwf_0"].Start(); break; // Multi Thread (1 per action) case WatchMode.MultiThreaded_A: ReadWatchList = new List <RAMValue>(); WriteWatchList = new List <RAMValue>(); FreezeWatchList = new List <RAMValue>(); _threads.Add("read_0", new Thread(new ThreadStart(delegate() { ThreadedWatchRead(); }))); _threads.Add("write_0", new Thread(new ThreadStart(delegate() { ThreadedWatchWrite(); }))); _threads.Add("freeze_0", new Thread(new ThreadStart(delegate() { ThreadedWatchFreeze(); }))); break; // Multi Thread (N Thread per action) case WatchMode.MultiThreaded_N: // TODO break; // Multi Thread (N Single Thread) case WatchMode.MultiThreaded_S: // TODO break; } // Start Queue Manager thread _threads.Add("queue_0", new Thread(new ThreadStart(delegate() { Dispatch(dispatchTimeout); }))); _threads["queue_0"].Start(); }
public static void Main(string[] args) { var passedArguments = new Arguments(args); IDatabase database = new SQLite(@"C:\git\personal\WatchMyPrices\myQuickDb.sqlite"); IWebQueryable webQueryable = new WebQueryable(); IPriceQueryable webPriceQueryable = new WebPriceQueryable(webQueryable, database); IInteractionMode mode = null; if (passedArguments.IsWatchMode) { mode = new WatchMode(webPriceQueryable, database, passedArguments.TimeInterval); } else { mode = new InteractiveMode(database); } mode.Run(); }
public void FileChange(WatchMode mode, int? ms) { TimeSpan? ts = null; if (ms != null) ts = TimeSpan.FromMilliseconds(ms.Value); string file = Path.GetTempFileName(); File.WriteAllBytes(file, new byte[]{1, 2, 3}); var m = new FileMonitor(file, new byte[] { 1, 2, 3 }, mode, ts); var wait = new ManualResetEvent(false); m.Changed += (a, e) => { wait.Set(); }; var t = Task.Factory.StartNew(() => { Thread.Sleep(1000); using (var fs = new FileStream(file, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)) { fs.Position = 2; fs.WriteByte(1); fs.Close(); }; }, TaskCreationOptions.LongRunning); Task.WaitAll(t); Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed"); wait.Reset(); m.Changed += (a, e) => { wait.Set(); }; Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed"); }