Skip to content

Commit 175110d

Browse files
authored
Merge pull request #17 from dotnet-campus/t/lindexi/ChuneleabiLacemcerebawhay
支持.NET 4.5 版本
2 parents ba777ff + 0d35e9c commit 175110d

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

AsyncWorkerCollection/AsyncAutoResetEvent.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ public AsyncAutoResetEvent(bool initialState)
1919
_isSignaled = initialState;
2020
}
2121

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+
2329

2430
/// <summary>
2531
/// 异步等待一个信号,需要await
@@ -31,6 +37,8 @@ public Task WaitOneAsync()
3137
{
3238
if (_isSignaled)
3339
{
40+
// 按照 AutoResetEvent 的设计,在没有任何等待进入时,如果有设置 Set 方法,那么下一次第一个进入的等待将会通过
41+
// 也就是在没有任何等待时,无论调用多少次 Set 方法,在调用之后只有一个等待通过
3442
_isSignaled = false;
3543
return CompletedSourceTask;
3644
}
@@ -42,7 +50,7 @@ public Task WaitOneAsync()
4250
}
4351

4452
/// <summary>
45-
/// 设置一个信号量,让一个waitone获得信号
53+
/// 设置一个信号量,让一个waitone获得信号,每次调用 <see cref="Set"/> 方法最多只有一个等待通过
4654
/// </summary>
4755
public void Set()
4856
{
@@ -71,6 +79,9 @@ public void Set()
7179
private readonly Queue<TaskCompletionSource<bool>> _waitQueue =
7280
new Queue<TaskCompletionSource<bool>>();
7381

82+
/// <summary>
83+
/// 用于在没有任何等待时让下一次等待通过
84+
/// </summary>
7485
private bool _isSignaled;
7586
}
7687
}

AsyncWorkerCollection/AsyncQueue.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66

7+
#if NETFRAMEWORK
8+
using ValueTask = System.Threading.Tasks.Task;
9+
#endif
10+
711
namespace dotnetCampus.Threading
812
{
913
/// <summary>
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFrameworks>net45;netcoreapp3.1</TargetFrameworks>
55
<RootNamespace>dotnetCampus.Threading</RootNamespace>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<AssemblyName>dotnetCampus.AsyncWorkerCollection</AssemblyName>
88
</PropertyGroup>
9+
10+
<ItemGroup Condition="'$(TargetFramework)'!='net45'">
11+
<Compile Remove="IAsyncDisposable.cs" />
12+
<Compile Remove="ConcurrentQueueExtension.cs" />
13+
</ItemGroup>
914
<ItemGroup>
10-
<PackageReference Include="dotnetCampus.SourceYard" Version="0.1.19099-alpha">
15+
<PackageReference Include="dotnetCampus.SourceYard" Version="0.1.19353-alpha">
1116
<PrivateAssets>all</PrivateAssets>
1217
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1318
</PackageReference>
1419
</ItemGroup>
20+
<ItemGroup Condition="'$(TargetFramework)'=='net45'">
21+
<PackageReference Include="System.ValueTuple" Version="4.5">
22+
23+
</PackageReference>
24+
</ItemGroup>
1525
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

0 commit comments

Comments
 (0)