Can wait for disconnection
This commit is contained in:
parent
d18142e950
commit
a812f6fdd2
@ -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>
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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>
|
||||
|
||||
@ -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]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user