I wrote an intentional infinite loop to identify if card is detected or removed on the reader using vb.net. Because I don't know if there is something like waitForCardPresent in winscard.
So the concept is once the button Connect was clicked, the ifinite loop, to check for card, will start.
So my code in backgroundworker looks like this:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Do
Dim ret As Integer
Dim rs As WinscardFun.SCARDREADER_STATE
rs.dwEventState = Nothing
rs.rgbAtr = Nothing
rs.pvUserData = Nothing
rs.cbAtr = Nothing
rs.szReader = rdr
rs.dwCurrentState = &H0
ret = WinscardFun.SCardGetStatusChange(hcontext, -1, rs, 1)
If ret = 0 And rs.cbAtr > 0 And rs.rgbAtr IsNot Nothing Then
ReDim atr(rs.cbAtr)
Array.Copy(rs.rgbAtr, atr, rs.cbAtr)
If status = "Connected" Then
'Do nothing
Else
test = "Connected"
Console.Write(test)
End If
Else
If test = "Remove" Then
'Do nothing
Else
status = "Remove"
Console.Write(test)
End If
End If
Loop
End Sub
This code explains that the program will continuously check for card status in background. I wrote the:
If status = "Connected" Then
'Do nothing
Else
test = "Connected"
Console.Write(test)
End If
Else
If test = "Remove" Then
'Do nothing
Else
status = "Removed"
Console.Write(test)
End If
End If
So my program won't flood with status and will only print connected or removed once.
So my questions are:
- Does the concept of javaxx smartcardio's waitforcardpresent() and waitForCardAbsent() is same with my idea?
- Are there much better code to detect for card status using vb.net/winscard? If yes, how?
- How can I stop this infinite loop? let's say the user click the Disconnect button?
- Does my infinite loop bad?
EDIT Infinite loop is not a good idea. So how can I detect a card status in background without consuming resources? or How should I properly use the scardgetstatuschange?
I though that the -1 in
WinscardFun.SCardGetStatusChange(hcontext, -1, rs, 1)
means it will loop infinite based on
''' <summary>
''' The SCardGetStatusChange function blocks execution until the current availability of the cards in a specific set of readers changes.
''' </summary>
''' <param name="hContext">A handle that identifies the resource manager context. The resource manager context is set by a previous call to the SCardEstablishContext function.</param>
''' <param name="TimeOut">The maximum amount of time, in milliseconds, to wait for an action. A value of zero causes the function to return immediately. A value of INFINITE causes this function never to time out.</param>
''' <param name="ReaderState">
''' An array of SCARD_READERSTATE structures that specify the readers to watch, and that receives the result.
''' To be notified of the arrival of a new smart card reader, set the szReader member of a SCARD_READERSTATE structure to "\\\\?PnP?\\Notification", and set all of the other members of that structure to zero.
''' Important Each member of each structure in this array must be initialized to zero and then set to specific values as necessary. If this is not done, the function will fail in situations that involve remote card readers.
''' </param>
''' <param name="ReaderCount">The number of elements in the rgReaderStates array.</param>
''' <returns></returns>
<DllImport("winscard.dll", EntryPoint:="SCardGetStatusChangeA", CharSet:=CharSet.Ansi)> _
Public Shared Function SCardGetStatusChange(ByVal hContext As IntPtr, ByVal TimeOut As Integer, ByRef ReaderState As SCARD_READERSTATE, ByVal ReaderCount As Integer) As Integer
End Function