1
- using FindDuplicateFiles . Common ;
2
- using System ;
1
+ using System ;
3
2
using System . Collections . Concurrent ;
4
3
using System . Collections . Generic ;
5
4
using System . Threading ;
@@ -42,9 +41,13 @@ public class CheckDuplicateQueue
42
41
/// 搜索完成
43
42
/// </summary>
44
43
public Action EventSearchFinished ;
44
+
45
+ /// <summary>
46
+ /// 正在执行的任务总数
47
+ /// </summary>
48
+ private int _searchingCount ;
45
49
public async void Start ( SearchMatchEnum searchMatch )
46
50
{
47
-
48
51
_isStop = false ;
49
52
_isFinished = false ;
50
53
EventMessage ? . Invoke ( "准备查找...." ) ;
@@ -57,7 +60,6 @@ await Task.Run(() =>
57
60
{
58
61
if ( _isFinished )
59
62
{
60
- EventSearchFinished . Invoke ( ) ;
61
63
return ;
62
64
}
63
65
continue ;
@@ -76,14 +78,15 @@ await Task.Run(() =>
76
78
/// <param name="fileInfo"></param>
77
79
private async void SearchDuplicate ( SimpleFileInfo fileInfo )
78
80
{
81
+ Interlocked . Increment ( ref _searchingCount ) ;
79
82
await MySemaphoreSlim . WaitAsync ( ) ;
80
83
try
81
84
{
82
85
if ( _isStop )
83
86
{
84
87
return ;
85
88
}
86
- EventMessage ? . Invoke ( $ "重复校验 :{ fileInfo . Path } ") ;
89
+ EventMessage ? . Invoke ( $ "检查文件 :{ fileInfo . Path } ") ;
87
90
string fileKey = "" ;
88
91
if ( ( _searchMatch & SearchMatchEnum . Name ) == SearchMatchEnum . Name )
89
92
{
@@ -107,7 +110,7 @@ private async void SearchDuplicate(SimpleFileInfo fileInfo)
107
110
}
108
111
109
112
var newFile = new List < SimpleFileInfo > { fileInfo } ;
110
- var resultFile = _duplicateFiles . AddOrUpdate ( fileKey , newFile , ( x , oldValue ) =>
113
+ var resultFile = _duplicateFiles . AddOrUpdate ( fileKey , newFile , ( _ , oldValue ) =>
111
114
{
112
115
oldValue . Add ( fileInfo ) ;
113
116
return oldValue ;
@@ -127,6 +130,10 @@ private async void SearchDuplicate(SimpleFileInfo fileInfo)
127
130
finally
128
131
{
129
132
MySemaphoreSlim . Release ( ) ;
133
+ if ( Interlocked . Decrement ( ref _searchingCount ) == 0 && _isFinished )
134
+ {
135
+ EventSearchFinished . Invoke ( ) ;
136
+ }
130
137
}
131
138
}
132
139
0 commit comments