PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Scard
oleobject loo_StReaders
oleobject loo_Json
oleobject loo_Task
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loo_Scard = create oleobject
li_rc = loo_Scard.ConnectToNewObject("Chilkat.SCard")
if li_rc < 0 then
destroy loo_Scard
MessageBox("Error","Connecting to COM object failed")
return
end if
// First establish a context to the PC/SC Resource Manager
li_Success = loo_Scard.EstablishContext("user")
if li_Success = 0 then
Write-Debug loo_Scard.LastErrorText
destroy loo_Scard
return
end if
// Get the list of all readers.
loo_StReaders = create oleobject
li_rc = loo_StReaders.ConnectToNewObject("Chilkat.StringTable")
li_Success = loo_Scard.ListReaders(loo_StReaders)
if li_Success = 0 then
Write-Debug loo_Scard.LastErrorText
destroy loo_Scard
destroy loo_StReaders
return
end if
// Create a Chilkat task to wait for a max of 1 hour (3600 seconds, or 3600000 milliseconds) for any smart card reader status change.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Task = loo_Scard.GetStatusChangeAsync(3600000,loo_StReaders,loo_Json)
if loo_Scard.LastMethodSuccess = 0 then
Write-Debug loo_Scard.LastErrorText
destroy loo_Scard
destroy loo_StReaders
destroy loo_Json
return
end if
// Start the task in a background thread.
li_Success = loo_Task.Run()
if not li_Success then
Write-Debug loo_Task.LastErrorText
end if
// 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..
do while loo_Task.Finished <> 1
// Sleep 100 ms.
loo_Task.SleepMs(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.
li_Success = loo_Task.GetResultBool()
if li_Success = 0 then
// The call to GetStatusChange in the background thread failed. Let's find out why by getting the LastErrorText
// for the background synchronous call.
Write-Debug loo_Task.ResultErrorText
end if
destroy loo_Task
// If the background call to GetStatusChange succeeded, then the result was placed in the last arg,
// which was our variable named "json".
if li_Success = 0 then
destroy loo_Scard
destroy loo_StReaders
destroy loo_Json
return
end if
// Let's see what happened...
loo_Json.EmitCompact = 0
Write-Debug loo_Json.Emit()
Write-Debug " "
// See the Wait for Smart Card Insertion/Removal Example for details about parsing the returned JSON.
// Applications should always release the context when finished.
li_Success = loo_Scard.ReleaseContext()
if li_Success = 0 then
Write-Debug loo_Scard.LastErrorText
end if
// 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
destroy loo_Scard
destroy loo_StReaders
destroy loo_Json