Can wait for disconnection

This commit is contained in:
nicolas.dorier 2023-10-23 09:03:11 +09:00
parent d18142e950
commit a812f6fdd2
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
4 changed files with 32 additions and 3 deletions

View File

@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.10</Version>
<Version>1.0.11</Version>
</PropertyGroup>
<PropertyGroup>

View File

@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using PCSC;
using PCSC.Extensions;
@ -112,6 +113,33 @@ waitStateChange:
}, TaskCreationOptions.LongRunning);
}
public Task WaitForDisconnected(CancellationToken cancellationToken = default)
{
return Task.Factory.StartNew(() =>
{
using var registration = cancellationToken.Register(() => Context.Cancel());
IntPtr timeout = IntPtr.Zero;
var readerStates = new[]
{
new SCardReaderState()
{
ReaderName = CardReader.ReaderName,
CurrentState = SCRState.Unaware
}
};
waitStateChange:
var res = Context.GetStatusChange(timeout, readerStates);
timeout = SCardContext.INFINITE;
readerStates[0].CurrentStateValue = readerStates[0].EventStateValue;
if (res == SCardError.Cancelled)
throw new OperationCanceledException(cancellationToken);
if (res != SCardError.Success)
return;
if (readerStates[0].EventState.CardIsAbsent())
return;
goto waitStateChange;
}, TaskCreationOptions.LongRunning);
}
public void Dispose()
{
CardReader.Dispose();

View File

@ -5,7 +5,7 @@
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<Version>1.0.10</Version>
<Version>1.0.11</Version>
</PropertyGroup>
<PropertyGroup>

View File

@ -207,7 +207,8 @@ public class UnitTest1
[Fact]
public async Task CanWaitForCard()
{
await PCSCContext.WaitForCard();
using var ctx = await PCSCContext.WaitForCard();
await ctx.WaitForDisconnected();
}
[Fact]