Skip to content

Commit

Permalink
Merge pull request #5 from PhantomGamers/clean
Browse files Browse the repository at this point in the history
Address code violations
  • Loading branch information
d2phap authored Jun 8, 2022
2 parents d0b669d + 5b8bc00 commit 6c0a1ad
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 91 deletions.
6 changes: 3 additions & 3 deletions Source/Demo/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions Source/Demo/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Form1()



private void btnStart_Click(object sender, EventArgs e)
private void BtnStart_Click(object sender, EventArgs e)
{
_fw = new FileSystemWatcherEx(txtPath.Text.Trim());

Expand All @@ -34,7 +34,7 @@ private void btnStart_Click(object sender, EventArgs e)
btnStop.Enabled = true;
}

private void FW_OnError(object sender, ErrorEventArgs e)
private void FW_OnError(object? sender, ErrorEventArgs e)
{
if (txtConsole.InvokeRequired)
{
Expand All @@ -46,28 +46,28 @@ private void FW_OnError(object sender, ErrorEventArgs e)
}
}

private void FW_OnChanged(object sender, FileChangedEvent e)
private void FW_OnChanged(object? sender, FileChangedEvent e)
{
txtConsole.Text += string.Format("[cha] {0} | {1}",
Enum.GetName(typeof(ChangeType), e.ChangeType),
e.FullPath) + "\r\n";
}

private void FW_OnDeleted(object sender, FileChangedEvent e)
private void FW_OnDeleted(object? sender, FileChangedEvent e)
{
txtConsole.Text += string.Format("[del] {0} | {1}",
Enum.GetName(typeof(ChangeType), e.ChangeType),
e.FullPath) + "\r\n";
}

private void FW_OnCreated(object sender, FileChangedEvent e)
private void FW_OnCreated(object? sender, FileChangedEvent e)
{
txtConsole.Text += string.Format("[cre] {0} | {1}",
Enum.GetName(typeof(ChangeType), e.ChangeType),
e.FullPath) + "\r\n";
}

private void FW_OnRenamed(object sender, FileChangedEvent e)
private void FW_OnRenamed(object? sender, FileChangedEvent e)
{
txtConsole.Text += string.Format("[ren] {0} | {1} ----> {2}",
Enum.GetName(typeof(ChangeType), e.ChangeType),
Expand All @@ -77,7 +77,7 @@ private void FW_OnRenamed(object sender, FileChangedEvent e)



private void btnStop_Click(object sender, EventArgs e)
private void BtnStop_Click(object sender, EventArgs e)
{
_fw.Stop();

Expand All @@ -88,7 +88,7 @@ private void btnStop_Click(object sender, EventArgs e)
}


private void btnSelectFolder_Click(object sender, EventArgs e)
private void BtnSelectFolder_Click(object sender, EventArgs e)
{
var fb = new FolderBrowserDialog();

Expand Down
2 changes: 1 addition & 1 deletion Source/FileWatcherEx/FileEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class FileChangedEvent
/// <summary>
/// The old full path (used if ChangeType = RENAMED)
/// </summary>
public string OldFullPath { get; set; } = "";
public string? OldFullPath { get; set; } = "";
}


140 changes: 87 additions & 53 deletions Source/FileWatcherEx/FileSystemWatcherEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

namespace FileWatcherEx;

/// <summary>
/// A wrapper of <see cref="FileSystemWatcher"/> to standardize the events
/// and avoid false change notifications.
/// </summary>
public class FileSystemWatcherEx : IDisposable
{

#region Private Properties

private Thread _thread;
private EventProcessor _processor;
private BlockingCollection<FileChangedEvent> _fileEventQueue = new();
private Thread? _thread;
private EventProcessor? _processor;
private readonly BlockingCollection<FileChangedEvent> _fileEventQueue = new();

private FileWatcher _watcher = new();
private FileSystemWatcher _fsw = new();
private FileWatcher? _watcher;
private FileSystemWatcher? _fsw;

// Define the cancellation token.
private CancellationTokenSource _cancelSource = new();
private readonly CancellationTokenSource _cancelSource = new();

#endregion

Expand All @@ -26,26 +30,23 @@ public class FileSystemWatcherEx : IDisposable
#region Public Properties

/// <summary>
/// Folder path to watch
/// Gets or sets the path of the directory to watch.
/// </summary>
public string FolderPath { get; set; } = "";


/// <summary>
/// The collection of all the filters used to determine what files are monitored in a directory.
/// Gets the collection of all the filters used to determine what files are monitored in a directory.
/// </summary>
public System.Collections.ObjectModel.Collection<string> Filters { get; } = new();


/// <summary>
/// Filter string used for determining what files are monitored in a directory
/// Gets or sets the filter string used to determine what files are monitored in a directory.
/// </summary>
public string Filter
{
get
{
return Filters.Count == 0 ? "*" : Filters[0];
}
get => Filters.Count == 0 ? "*" : Filters[0];
set
{
Filters.Clear();
Expand All @@ -55,7 +56,11 @@ public string Filter


/// <summary>
/// Gets, sets the type of changes to watch for
/// Gets or sets the type of changes to watch for.
/// The default is the bitwise OR combination of
/// <see cref="NotifyFilters.LastWrite"/>,
/// <see cref="NotifyFilters.FileName"/>,
/// and <see cref="NotifyFilters.DirectoryName"/>.
/// </summary>
public NotifyFilters NotifyFilter { get; set; } = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

Expand All @@ -69,38 +74,64 @@ public string Filter
/// <summary>
/// Gets or sets the object used to marshal the event handler calls issued as a result of a directory change.
/// </summary>
public ISynchronizeInvoke SynchronizingObject { get; set; }
public ISynchronizeInvoke? SynchronizingObject { get; set; }

#endregion



#region Public Events
public delegate void DelegateOnChanged(object sender, FileChangedEvent e);
public event DelegateOnChanged OnChanged;

public delegate void DelegateOnDeleted(object sender, FileChangedEvent e);
public event DelegateOnDeleted OnDeleted;
/// <summary>
/// Occurs when a file or directory in the specified
/// <see cref="FolderPath"/> is changed.
/// </summary>
public event DelegateOnChanged? OnChanged;
public delegate void DelegateOnChanged(object? sender, FileChangedEvent e);


/// <summary>
/// Occurs when a file or directory in the specified
/// <see cref="FolderPath"/> is deleted.
/// </summary>
public event DelegateOnDeleted? OnDeleted;
public delegate void DelegateOnDeleted(object? sender, FileChangedEvent e);

public delegate void DelegateOnCreated(object sender, FileChangedEvent e);
public event DelegateOnCreated OnCreated;

public delegate void DelegateOnRenamed(object sender, FileChangedEvent e);
public event DelegateOnRenamed OnRenamed;
/// <summary>
/// Occurs when a file or directory in the specified
/// <see cref="FolderPath"/> is created.
/// </summary>
public event DelegateOnCreated? OnCreated;
public delegate void DelegateOnCreated(object? sender, FileChangedEvent e);


/// <summary>
/// Occurs when a file or directory in the specified
/// <see cref="FolderPath"/> is renamed.
/// </summary>
public event DelegateOnRenamed? OnRenamed;
public delegate void DelegateOnRenamed(object? sender, FileChangedEvent e);


/// <summary>
/// Occurs when the instance of <see cref="FileSystemWatcherEx"/> is unable to continue
/// monitoring changes or when the internal buffer overflows.
/// </summary>
public event DelegateOnError? OnError;
public delegate void DelegateOnError(object? sender, ErrorEventArgs e);

public delegate void DelegateOnError(object sender, ErrorEventArgs e);
public event DelegateOnError OnError;
#endregion



/// <summary>
/// Initialize new instance of FileWatcherEx
/// Initialize new instance of <see cref="FileSystemWatcherEx"/>
/// </summary>
/// <param name="folder"></param>
public FileSystemWatcherEx(string folder = "")
/// <param name="folderPath"></param>
public FileSystemWatcherEx(string folderPath = "")
{
FolderPath = folder;
FolderPath = folderPath;
}


Expand All @@ -120,7 +151,7 @@ public void Start()
InvokeChangedEvent(SynchronizingObject, e);
void InvokeChangedEvent(object sender, FileChangedEvent fileEvent)
void InvokeChangedEvent(object? sender, FileChangedEvent fileEvent)
{
if (SynchronizingObject != null && SynchronizingObject.InvokeRequired)
{
Expand All @@ -139,7 +170,7 @@ void InvokeChangedEvent(object sender, FileChangedEvent fileEvent)
InvokeCreatedEvent(SynchronizingObject, e);
void InvokeCreatedEvent(object sender, FileChangedEvent fileEvent)
void InvokeCreatedEvent(object? sender, FileChangedEvent fileEvent)
{
if (SynchronizingObject != null && SynchronizingObject.InvokeRequired)
{
Expand All @@ -158,7 +189,7 @@ void InvokeCreatedEvent(object sender, FileChangedEvent fileEvent)
InvokeDeletedEvent(SynchronizingObject, e);
void InvokeDeletedEvent(object sender, FileChangedEvent fileEvent)
void InvokeDeletedEvent(object? sender, FileChangedEvent fileEvent)
{
if (SynchronizingObject != null && SynchronizingObject.InvokeRequired)
{
Expand All @@ -177,7 +208,7 @@ void InvokeDeletedEvent(object sender, FileChangedEvent fileEvent)
InvokeRenamedEvent(SynchronizingObject, e);
void InvokeRenamedEvent(object sender, FileChangedEvent fileEvent)
void InvokeRenamedEvent(object? sender, FileChangedEvent fileEvent)
{
if (SynchronizingObject != null && SynchronizingObject.InvokeRequired)
{
Expand Down Expand Up @@ -244,25 +275,6 @@ void onError(ErrorEventArgs e)
_fsw.EnableRaisingEvents = true;
}

private void Thread_DoingWork(CancellationToken cancelToken)
{
while (true)
{
if (cancelToken.IsCancellationRequested)
return;

try
{
var e = _fileEventQueue.Take(cancelToken);
_processor.ProcessEvent(e);
}
catch (OperationCanceledException)
{
return;
}
}
}


/// <summary>
/// Stop watching files
Expand All @@ -284,7 +296,6 @@ public void Stop()
}



/// <summary>
/// Dispose the FileWatcherEx instance
/// </summary>
Expand All @@ -293,7 +304,30 @@ public void Dispose()
if (_fsw != null)
{
_fsw.Dispose();
GC.SuppressFinalize(this);
}
}



private void Thread_DoingWork(CancellationToken cancelToken)
{
while (true)
{
if (cancelToken.IsCancellationRequested)
return;

try
{
var e = _fileEventQueue.Take(cancelToken);
_processor?.ProcessEvent(e);
}
catch (OperationCanceledException)
{
return;
}
}
}


}
Loading

0 comments on commit 6c0a1ad

Please sign in to comment.