Skip to content
Permalink
Browse files

DirectoryTreeWatcher - update sample program; thread safe create

  • Loading branch information
bobbymcr committed Feb 12, 2020
1 parent 6cf08c7 commit 159f05497cf39cd7c2d5c3c137b8bca186b0a5bf
@@ -16,8 +16,12 @@ internal sealed class Program

private static void Main()
{
Directory.CreateDirectory("inner1");
Directory.CreateDirectory("inner2");

using CancellationTokenSource cts = new CancellationTokenSource();
string[] files = new string[] { "file1.txt", "file2.txt" };
string[] files = new string[] { @"inner1\file1.txt", @"inner2\file2.txt" };

Task task = UpdateFilesAsync(files, cts.Token);

RunWatcher(files);
@@ -31,7 +35,7 @@ private static void Main()

private static void RunWatcher(string[] files)
{
using DirectoryWatcher watcher = new DirectoryWatcher(new DirectoryInfo("."));
using DirectoryTreeWatcher watcher = new DirectoryTreeWatcher(new DirectoryInfo("."));

Action<FileInfo> onUpdated = f => Log($"Got an update for '{f.Name}'");

@@ -0,0 +1,18 @@
// <copyright file="DirectoryTreeWatcher.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace DirectoryWatcherSample
{
using System.IO;

public sealed class DirectoryTreeWatcher : DirectoryTreeWatcherBase
{
public DirectoryTreeWatcher(DirectoryInfo path)
: base(path)
{
}

protected override DirectoryWatcherBase Create(DirectoryInfo path) => new DirectoryWatcher(path);
}
}
@@ -11,7 +11,7 @@ namespace DirectoryWatcherSample
public abstract class DirectoryTreeWatcherBase : IDisposable
{
private readonly string path;
private readonly ConcurrentDictionary<string, DirectoryWatcherBase> watchers;
private readonly ConcurrentDictionary<string, Watcher> watchers;

protected DirectoryTreeWatcherBase(DirectoryInfo path)
{
@@ -21,7 +21,7 @@ protected DirectoryTreeWatcherBase(DirectoryInfo path)
}

this.path = path.FullName + "\\";
this.watchers = new ConcurrentDictionary<string, DirectoryWatcherBase>(StringComparer.OrdinalIgnoreCase);
this.watchers = new ConcurrentDictionary<string, Watcher>(StringComparer.OrdinalIgnoreCase);
}

public IDisposable Subscribe(string file, Action<FileInfo> onUpdate)
@@ -46,14 +46,14 @@ public IDisposable Subscribe(string file, Action<FileInfo> onUpdate)
nameof(file));
}

DirectoryWatcherBase watcher = this.watchers.GetOrAdd(key, k => this.Create(dir));
Watcher watcher = this.watchers.GetOrAdd(key, k => new Watcher(() => this.Create(dir)));
return watcher.Subscribe(fullPath.Name, onUpdate);
}

public void Dispose()
{
this.Dispose(true);
foreach (DirectoryWatcherBase watcher in this.watchers.Values)
foreach (Watcher watcher in this.watchers.Values)
{
watcher.Dispose();
}
@@ -66,5 +66,22 @@ public void Dispose()
protected virtual void Dispose(bool disposing)
{
}

private readonly struct Watcher : IDisposable
{
private readonly Lazy<DirectoryWatcherBase> inner;

public Watcher(Func<DirectoryWatcherBase> create)
{
this.inner = new Lazy<DirectoryWatcherBase>(create);
}

public void Dispose() => this.inner.Value.Dispose();

public IDisposable Subscribe(string name, Action<FileInfo> onUpdate)
{
return this.inner.Value.Subscribe(name, onUpdate);
}
}
}
}

0 comments on commit 159f054

Please sign in to comment.
You can’t perform that action at this time.