Skip to content

Commit

Permalink
fixed: calling Stop() throws ObjectDisposed exception
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Mar 30, 2023
1 parent 41c9564 commit 0e7ca5b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Source/Demo/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private void BtnStart_Click(object sender, EventArgs e)

_fw.Start();

btnStart.Enabled = false;
btnStart.Enabled = true;
btnSelectFolder.Enabled = false;
txtPath.Enabled = false;
btnStop.Enabled = true;
Expand Down Expand Up @@ -89,6 +89,7 @@ private void BtnStop_Click(object sender, EventArgs e)
btnSelectFolder.Enabled = true;
txtPath.Enabled = true;
btnStop.Enabled = false;
btnStop.Enabled = true;
}


Expand Down
1 change: 0 additions & 1 deletion Source/FileSystemEventRecorder/FileSystemEventRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics;
using System.Globalization;
using CsvHelper;
using FileWatcherEx;

namespace FileSystemEventRecorder;

Expand Down
8 changes: 5 additions & 3 deletions Source/FileWatcherEx/FileSystemWatcherEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public FileSystemWatcherEx(string folderPath = "", Action<string>? logger = null
public void Start()
{
if (!Directory.Exists(FolderPath)) return;
Stop();


_processor = new EventProcessor((e) =>
Expand Down Expand Up @@ -293,10 +294,11 @@ internal void StartForTesting(
public void Stop()
{
_watcher?.Dispose();
_watcher = null;

// stop the thread
_cancelSource?.Cancel();
_cancelSource?.Dispose();
_cancelSource = null;
}


Expand All @@ -305,8 +307,8 @@ public void Stop()
/// </summary>
public void Dispose()
{
_watcher?.Dispose();
_cancelSource?.Dispose();
Stop();

GC.SuppressFinalize(this);
}

Expand Down
9 changes: 7 additions & 2 deletions Source/FileWatcherEx/FileWatcherEx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@
<Description>A wrapper of FileSystemWatcher to standardize the events and avoid false change notifications, used in ImageGlass project (https://imageglass.org). This project is based on the VSCode FileWatcher: https://github.com/Microsoft/vscode-filewatcher-windows.</Description>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<FileVersion>$(Version)</FileVersion>
<VersionPrefix>2.3.0</VersionPrefix>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<VersionPrefix>2.4.0</VersionPrefix>
<PackageReleaseNotes>See https://github.com/d2phap/FileWatcherEx/releases</PackageReleaseNotes>
<Authors>d2phap</Authors>
<Title>FileWatcherEx - A file system watcher</Title>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<SignAssembly>False</SignAssembly>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
Expand Down

0 comments on commit 0e7ca5b

Please sign in to comment.