Skip to content

Commit 0e7ca5b

Browse files
committed
fixed: calling Stop() throws ObjectDisposed exception
1 parent 41c9564 commit 0e7ca5b

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

Source/Demo/Form1.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private void BtnStart_Click(object sender, EventArgs e)
2929

3030
_fw.Start();
3131

32-
btnStart.Enabled = false;
32+
btnStart.Enabled = true;
3333
btnSelectFolder.Enabled = false;
3434
txtPath.Enabled = false;
3535
btnStop.Enabled = true;
@@ -89,6 +89,7 @@ private void BtnStop_Click(object sender, EventArgs e)
8989
btnSelectFolder.Enabled = true;
9090
txtPath.Enabled = true;
9191
btnStop.Enabled = false;
92+
btnStop.Enabled = true;
9293
}
9394

9495

Source/FileSystemEventRecorder/FileSystemEventRecorder.cs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Diagnostics;
33
using System.Globalization;
44
using CsvHelper;
5-
using FileWatcherEx;
65

76
namespace FileSystemEventRecorder;
87

Source/FileWatcherEx/FileSystemWatcherEx.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public FileSystemWatcherEx(string folderPath = "", Action<string>? logger = null
149149
public void Start()
150150
{
151151
if (!Directory.Exists(FolderPath)) return;
152+
Stop();
152153

153154

154155
_processor = new EventProcessor((e) =>
@@ -293,10 +294,11 @@ internal void StartForTesting(
293294
public void Stop()
294295
{
295296
_watcher?.Dispose();
297+
_watcher = null;
296298

297299
// stop the thread
298-
_cancelSource?.Cancel();
299300
_cancelSource?.Dispose();
301+
_cancelSource = null;
300302
}
301303

302304

@@ -305,8 +307,8 @@ public void Stop()
305307
/// </summary>
306308
public void Dispose()
307309
{
308-
_watcher?.Dispose();
309-
_cancelSource?.Dispose();
310+
Stop();
311+
310312
GC.SuppressFinalize(this);
311313
}
312314

Source/FileWatcherEx/FileWatcherEx.csproj

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
1313
<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>
1414
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1515
<FileVersion>$(Version)</FileVersion>
16-
<VersionPrefix>2.3.0</VersionPrefix>
17-
<PackageLicenseExpression>MIT</PackageLicenseExpression>
16+
<VersionPrefix>2.4.0</VersionPrefix>
1817
<PackageReleaseNotes>See https://github.com/d2phap/FileWatcherEx/releases</PackageReleaseNotes>
1918
<Authors>d2phap</Authors>
2019
<Title>FileWatcherEx - A file system watcher</Title>
2120
<IncludeSymbols>True</IncludeSymbols>
2221
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
22+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
23+
<SignAssembly>False</SignAssembly>
2324
</PropertyGroup>
2425

2526
<ItemGroup>
27+
<None Include="..\..\LICENSE">
28+
<Pack>True</Pack>
29+
<PackagePath>\</PackagePath>
30+
</None>
2631
<None Include="..\..\README.md">
2732
<Pack>True</Pack>
2833
<PackagePath>\</PackagePath>

0 commit comments

Comments
 (0)