Skip to content

Commit f7705f3

Browse files
committed
#6 修改完成搜索的触发时机
1 parent d324813 commit f7705f3

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/SearchFile/CheckDuplicateQueue.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using FindDuplicateFiles.Common;
2-
using System;
1+
using System;
32
using System.Collections.Concurrent;
43
using System.Collections.Generic;
54
using System.Threading;
@@ -42,9 +41,13 @@ public class CheckDuplicateQueue
4241
/// 搜索完成
4342
/// </summary>
4443
public Action EventSearchFinished;
44+
45+
/// <summary>
46+
/// 正在执行的任务总数
47+
/// </summary>
48+
private int _searchingCount;
4549
public async void Start(SearchMatchEnum searchMatch)
4650
{
47-
4851
_isStop = false;
4952
_isFinished = false;
5053
EventMessage?.Invoke("准备查找....");
@@ -57,7 +60,6 @@ await Task.Run(() =>
5760
{
5861
if (_isFinished)
5962
{
60-
EventSearchFinished.Invoke();
6163
return;
6264
}
6365
continue;
@@ -76,14 +78,15 @@ await Task.Run(() =>
7678
/// <param name="fileInfo"></param>
7779
private async void SearchDuplicate(SimpleFileInfo fileInfo)
7880
{
81+
Interlocked.Increment(ref _searchingCount);
7982
await MySemaphoreSlim.WaitAsync();
8083
try
8184
{
8285
if (_isStop)
8386
{
8487
return;
8588
}
86-
EventMessage?.Invoke($"重复校验{fileInfo.Path}");
89+
EventMessage?.Invoke($"检查文件{fileInfo.Path}");
8790
string fileKey = "";
8891
if ((_searchMatch & SearchMatchEnum.Name) == SearchMatchEnum.Name)
8992
{
@@ -107,7 +110,7 @@ private async void SearchDuplicate(SimpleFileInfo fileInfo)
107110
}
108111

109112
var newFile = new List<SimpleFileInfo> { fileInfo };
110-
var resultFile = _duplicateFiles.AddOrUpdate(fileKey, newFile, (x, oldValue) =>
113+
var resultFile = _duplicateFiles.AddOrUpdate(fileKey, newFile, (_, oldValue) =>
111114
{
112115
oldValue.Add(fileInfo);
113116
return oldValue;
@@ -127,6 +130,10 @@ private async void SearchDuplicate(SimpleFileInfo fileInfo)
127130
finally
128131
{
129132
MySemaphoreSlim.Release();
133+
if (Interlocked.Decrement(ref _searchingCount) == 0 && _isFinished)
134+
{
135+
EventSearchFinished.Invoke();
136+
}
130137
}
131138
}
132139

0 commit comments

Comments
 (0)