Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.StringTable,
  Chilkat.Task,
  Chilkat.JsonObject,
  Chilkat.SCard;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  scard: TSCard;
  stReaders: TStringTable;
  json: TJsonObject;
  task: TTask;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  scard := TSCard.Create;

  //  First establish a context to the PC/SC Resource Manager
  success := scard.EstablishContext('user');
  if (success = False) then
    begin
      WriteLn(scard.LastErrorText);
      Exit;
    end;

  //  Get the list of all readers.
  stReaders := TStringTable.Create;
  success := scard.ListReaders(stReaders);
  if (success = False) then
    begin
      WriteLn(scard.LastErrorText);
      Exit;
    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.
  json := TJsonObject.Create;
  task := scard.GetStatusChangeAsync(3600000,stReaders,json);
  if (scard.LastMethodSuccess = False) then
    begin
      WriteLn(scard.LastErrorText);
      Exit;
    end;
  //  Start the task in a background thread.
  success := task.Run();
  if (not success) then
    begin
      WriteLn(task.LastErrorText);
    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 task.Finished <> True do
    begin

      //  Sleep 100 ms.  
      task.SleepMs(100);
    end;

  //  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.
  success := task.GetResultBool();
  if (success = False) then
    begin
      //  The call to GetStatusChange in the background thread failed.  Let's find out why by getting the LastErrorText
      //  for the background synchronous call.
      WriteLn(task.ResultErrorText);
    end;

  task.Free;

  //  If the background call to GetStatusChange succeeded, then the result was placed in the last arg,
  //  which was our variable named "json".
  if (success = False) then
    begin
      Exit;
    end;

  //  Let's see what happened...
  json.EmitCompact := False;
  WriteLn(json.Emit());
  WriteLn(' ');

  //  See the Wait for Smart Card Insertion/Removal Example for details about parsing the returned JSON.

  //  Applications should always release the context when finished.
  success := scard.ReleaseContext();
  if (success = False) then
    begin
      WriteLn(scard.LastErrorText);
    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


  scard.Free;
  stReaders.Free;
  json.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.