Example using CardReaderObserver to wait for a card service

Concept

  1. Start the observer.

  2. Create a service object

  3. Wait for a card respective the service to be available

  4. Before terminating Stop the observer.

Example using CardReaderObserver

Visual Basic Copy imageCopy
Sub Main()
   Try
      ' The service object
      Dim myService as MyService
      ' Start the BasicCard framework. Once
      ' started the card readers are monitored.
      CardReaderObserver.Instance.Start()
      ' Request a service object of MyService class.
      ' Wait for maximum 30 seconds.
      myService = _
          CardReaderObserver.Instance().WaitForCardService( _
                                new MyService(),
                                new TimeSpan(0,0,30)) // 0h, 0min, 30s
      If myService Is Nothing Then
         System.Console.WriteLine("No matching card was inserted.")
      Else
         ' Call functions provided by service.
         myService.DoWork();
      End If
   ' catch exceptions
   Catch e As CommsException
      e.Dump()
   Catch e As Exception
      System.Console.Error.WriteLine(e.ToString())
   Finally
      ' stop the BasicCard framework
      CardReaderObserver.Instance.Stop()
   End Try
End Sub
C# Copy imageCopy
static void Main(string[] args)
{
   try
   {
      // The service object
      MyService myService;
      // Start the BasicCard framework. Once
      // started the card readers are monitored.
      CardReaderObserver.Instance.Start();
      // Request a service object of MyService class.
      // Wait for maximum 30 seconds.
      myService =
          (myService) CardReaderObserver
                         .Instance
                         .WaitForCardService(
                                new MyService(),
                                new TimeSpan(0,0,30)); // 0h, 0min, 30s
      if (null == myService)
      {
         System.Console.WriteLine("No matching card was inserted.");
      }
      else
      {
         // Call functions provided by service.
         myService.DoWork();
      }
   }
   // catch exceptions
   catch (CommsException e)
   {
      e.Dump();
   }
   catch (Exception e)
   {
      System.Console.Error.WriteLine(e.ToString());
   }
   finally
   {
      // stop the BasicCard framework
      CardReaderObserver.Instance.Stop();
   }
}

See Also