File tree Expand file tree Collapse file tree 5 files changed +63
-4
lines changed Expand file tree Collapse file tree 5 files changed +63
-4
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,13 @@ public AsyncAutoResetEvent(bool initialState)
19
19
_isSignaled = initialState ;
20
20
}
21
21
22
- private static readonly Task CompletedSourceTask = Task . FromResult ( true ) ;
22
+ private static readonly Task CompletedSourceTask
23
+ #if NETFRAMEWORK
24
+ = Task . FromResult ( true ) ;
25
+ #else
26
+ = Task . CompletedTask ;
27
+ #endif
28
+
23
29
24
30
/// <summary>
25
31
/// 异步等待一个信号,需要await
@@ -31,6 +37,8 @@ public Task WaitOneAsync()
31
37
{
32
38
if ( _isSignaled )
33
39
{
40
+ // 按照 AutoResetEvent 的设计,在没有任何等待进入时,如果有设置 Set 方法,那么下一次第一个进入的等待将会通过
41
+ // 也就是在没有任何等待时,无论调用多少次 Set 方法,在调用之后只有一个等待通过
34
42
_isSignaled = false ;
35
43
return CompletedSourceTask ;
36
44
}
@@ -42,7 +50,7 @@ public Task WaitOneAsync()
42
50
}
43
51
44
52
/// <summary>
45
- /// 设置一个信号量,让一个waitone获得信号
53
+ /// 设置一个信号量,让一个waitone获得信号,每次调用 <see cref="Set"/> 方法最多只有一个等待通过
46
54
/// </summary>
47
55
public void Set ( )
48
56
{
@@ -71,6 +79,9 @@ public void Set()
71
79
private readonly Queue < TaskCompletionSource < bool > > _waitQueue =
72
80
new Queue < TaskCompletionSource < bool > > ( ) ;
73
81
82
+ /// <summary>
83
+ /// 用于在没有任何等待时让下一次等待通过
84
+ /// </summary>
74
85
private bool _isSignaled ;
75
86
}
76
87
}
Original file line number Diff line number Diff line change 4
4
using System . Threading ;
5
5
using System . Threading . Tasks ;
6
6
7
+ #if NETFRAMEWORK
8
+ using ValueTask = System . Threading . Tasks . Task ;
9
+ #endif
10
+
7
11
namespace dotnetCampus . Threading
8
12
{
9
13
/// <summary>
Original file line number Diff line number Diff line change 1
1
<Project Sdk =" Microsoft.NET.Sdk" >
2
2
3
3
<PropertyGroup >
4
- <TargetFramework > netcoreapp3.1</TargetFramework >
4
+ <TargetFrameworks >net45; netcoreapp3.1</TargetFrameworks >
5
5
<RootNamespace >dotnetCampus.Threading</RootNamespace >
6
6
<GeneratePackageOnBuild >true</GeneratePackageOnBuild >
7
7
<AssemblyName >dotnetCampus.AsyncWorkerCollection</AssemblyName >
8
8
</PropertyGroup >
9
+
10
+ <ItemGroup Condition =" '$(TargetFramework)'!='net45'" >
11
+ <Compile Remove =" IAsyncDisposable.cs" />
12
+ <Compile Remove =" ConcurrentQueueExtension.cs" />
13
+ </ItemGroup >
9
14
<ItemGroup >
10
- <PackageReference Include =" dotnetCampus.SourceYard" Version =" 0.1.19099 -alpha" >
15
+ <PackageReference Include =" dotnetCampus.SourceYard" Version =" 0.1.19353 -alpha" >
11
16
<PrivateAssets >all</PrivateAssets >
12
17
<IncludeAssets >runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets >
13
18
</PackageReference >
14
19
</ItemGroup >
20
+ <ItemGroup Condition =" '$(TargetFramework)'=='net45'" >
21
+ <PackageReference Include =" System.ValueTuple" Version =" 4.5" >
22
+
23
+ </PackageReference >
24
+ </ItemGroup >
15
25
</Project >
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Concurrent ;
3
+ using System . Collections . Generic ;
4
+ using System . Linq ;
5
+ using System . Text ;
6
+ using System . Threading . Tasks ;
7
+
8
+ namespace dotnetCampus . Threading
9
+ {
10
+ static class ConcurrentQueueExtension
11
+ {
12
+ // 在 .NET Framework 4.5 没有清理方法
13
+ public static void Clear < T > ( this ConcurrentQueue < T > queue )
14
+ {
15
+ while ( queue . TryDequeue ( out _ ) )
16
+ {
17
+
18
+ }
19
+ }
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Text ;
5
+ using System . Threading . Tasks ;
6
+
7
+ namespace dotnetCampus . Threading
8
+ {
9
+ // 这个接口在 .NET Framework 4.5 没有
10
+ interface IAsyncDisposable
11
+ {
12
+ }
13
+ }
You can’t perform that action at this time.
0 commit comments