DataFlex
DataFlex
PC/SC Async Wait for Smart Card Status Change (Inserted, Removed from Reader, etc.)
See more SCard Examples
Demonstrates how to start an asynchronous Chilkat task to wait for a status change, such as for a smart card to be inserted into a reader, or removed from a reader. After starting the background task, the code loops to check on the status of your task.Note: Instead of writing a loop to wait for the status change, your application might periodically check the task status via a timer event or something similar. The purpose of this example is to show (1) how to start the async task, and (2) how to periodically check the status of the task.
Note: This functionality was introduced in Chilkat v9.5.0.87.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoScard
Variant vStReaders
Handle hoStReaders
Variant vJson
Handle hoJson
Variant vTask
Handle hoTask
String sTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatSCard)) To hoScard
If (Not(IsComObjectCreated(hoScard))) Begin
Send CreateComObject of hoScard
End
// First establish a context to the PC/SC Resource Manager
Get ComEstablishContext Of hoScard "user" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoScard To sTemp1
Showln sTemp1
Procedure_Return
End
// Get the list of all readers.
Get Create (RefClass(cComChilkatStringTable)) To hoStReaders
If (Not(IsComObjectCreated(hoStReaders))) Begin
Send CreateComObject of hoStReaders
End
Get pvComObject of hoStReaders to vStReaders
Get ComListReaders Of hoScard vStReaders To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoScard To sTemp1
Showln sTemp1
Procedure_Return
End
// Create a Chilkat task to wait for a max of 1 hour (3600 seconds, or 3600000 milliseconds) for any smart card reader status change.
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get pvComObject of hoStReaders to vStReaders
Get pvComObject of hoJson to vJson
Get ComGetStatusChangeAsync Of hoScard 3600000 vStReaders vJson To vTask
If (IsComObject(vTask)) Begin
Get Create (RefClass(cComChilkatTask)) To hoTask
Set pvComObject Of hoTask To vTask
End
Get ComLastMethodSuccess Of hoScard To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoScard To sTemp1
Showln sTemp1
Procedure_Return
End
// Start the task in a background thread.
Get ComRun Of hoTask To iSuccess
If (Not iSuccess) Begin
Get ComLastErrorText Of hoTask To sTemp1
Showln sTemp1
End
// Loop until the task is finished, which happens when any reader's status changes.
// Instead of looping here, your application could periodically check on the task status in some other way,
// such as in a periodic timer event..
While ((ComFinished(hoTask)) <> True)
// Sleep 100 ms.
Send ComSleepMs To hoTask 100
Loop
// When we call GetStatusChangeAsync, what's really happening is that GetStatusChange is being called in a background thread.
// It returns a boolean (success/failure). Therefore, we call task.GetResultBool to get the boolean returned by GetStatusChange
// in the background thread.
Get ComGetResultBool Of hoTask To iSuccess
If (iSuccess = False) Begin
// The call to GetStatusChange in the background thread failed. Let's find out why by getting the LastErrorText
// for the background synchronous call.
Get ComResultErrorText Of hoTask To sTemp1
Showln sTemp1
End
Send Destroy of hoTask
// If the background call to GetStatusChange succeeded, then the result was placed in the last arg,
// which was our variable named "json".
If (iSuccess = False) Begin
Procedure_Return
End
// Let's see what happened...
Set ComEmitCompact Of hoJson To False
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
Showln " "
// See the Wait for Smart Card Insertion/Removal Example for details about parsing the returned JSON.
// Applications should always release the context when finished.
Get ComReleaseContext Of hoScard To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoScard To sTemp1
Showln sTemp1
End
// Note: It may be necessary to call FinalizeThreadPool in some programming environments just before your program exits.
// (Not after every async function call, but only before program exit.)
// See Call FinalizeThreadPool before program exit
End_Procedure