private static void Console_Execute_ReadReaderID(DocInterfaceControl _docIntControl) { //First make sure DocInterfaceControl is initialized if (_docIntControl != null) { if (_docIntControl.IsInitialized) { try { //Call ReadReaderID and show result (no "thread" function available for this call. Should not block for a long time) var readerID = _docIntControl.ReadReaderID(); if (readerID != null) { Console.WriteLine(""); Console.WriteLine("ReaderID:"); Console.WriteLine(readerID.ToString()); } } catch { Console.WriteLine("Exception"); } } else { Console.WriteLine("DocInterfaceControl not initialized!"); } } }
/* * SampleThreads_CSharp * SampleCode for iIDReaderLibrary.DocInterfaceControl * Implemented in C# * Using "Start..." functions * * This sample demonstrates how to call the DocInterfaceControl functions that run the process in a separate new thread. * This is only for demo purposes. For a Console application is not efficient to work in this way. */ static void Main(string[] args) { Console.WriteLine(".NETCore Console"); Console.WriteLine("SampleThreads_C#"); Console.WriteLine("--------------------"); Console.WriteLine("Library Version: " + iIDReaderLibrary.Version.LibraryVersion); //Get DocInterfaceControl instance DocInterfaceControl docIntControl = Console_InitializeDocInterfaceControl(); if (docIntControl != null) { //DocInterfaceControl is initialized Console.WriteLine(""); Console.Write("Detecting reader.."); while (true) { //First of all, get the Reader Information Console.Write("."); var readerID = docIntControl.ReadReaderID(); if (readerID != null) { Console.WriteLine(""); Console.WriteLine("Detected Reader:"); Console.WriteLine(readerID.ToString()); break; } } //Reader info obtained --> execute functions using menu Console.WriteLine(""); while (Console_ExecuteAndContinue(docIntControl)) { ; } docIntControl.Terminate(); Console.WriteLine(""); Console.Write("EXITING in 5"); Thread.Sleep(1000); Console.Write(", 4"); Thread.Sleep(1000); Console.Write(", 3"); Thread.Sleep(1000); Console.Write(", 2"); Thread.Sleep(1000); Console.Write(", 1"); Thread.Sleep(1000); } else { Console.Write("Initialization error <press ENTER to exit>"); Console.ReadLine(); } }
private static void Console_Execute_ReadReaderID(DocInterfaceControl _docIntControl) { //First make sure DocInterfaceControl is initialized if (_docIntControl != null) { if (_docIntControl.IsInitialized) { try { DateTime startTime = DateTime.UtcNow; //Call ReadReaderID and show result var readerID = _docIntControl.ReadReaderID(); TimeSpan processSpan = DateTime.UtcNow - startTime; if (readerID != null) { Console.WriteLine(""); Console.WriteLine("ReaderID:"); Console.WriteLine(readerID.ToString()); Console.WriteLine(string.Format("(Duration: {0})", processSpan)); } else { //Update result in UI Console.WriteLine(string.Format("Result: FAIL. Duration: {0}", processSpan)); } } catch { Console.WriteLine("Exception"); } } else { Console.WriteLine("DocInterfaceControl not initialized!"); } } }
private void Worker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; int readerCheckFailCount = 0; //Check readerID in 5 seconds interval TimeSpan readerCheckSpan = TimeSpan.FromSeconds(5); //Initialize lastChecked in the past --> when Worker started it should check for the ReaderID first of all DateTime lastCheckedOk = DateTime.UtcNow.AddMinutes(-1); //While port initialized: // Check reader communication is still possible in 5 seconds interval // Hint: This interval is not fixed! It is recomended to check the communication with the reader when no other operation is executed. // => Most important for battery powered devices! while (m_DocInterface.IsInitialized) { if (worker.CancellationPending == true) { //Exit loop if Background worker is cancelled e.Cancel = true; break; } else { if ((DateTime.UtcNow - lastCheckedOk) < readerCheckSpan) { //Next check time still not reached --> just do nothing continue; } else { //Next check time reached --> check ReaderID var readerInfo = m_DocInterface.ReadReaderID(); if (readerInfo != null) { //ReaderID check OK readerCheckFailCount = 0; lastCheckedOk = DateTime.UtcNow; if (!m_ReaderFound) { //Not previously found --> Enable functions m_ReaderFound = true; SetUiEnabled(true, readerInfo.ReaderID); //TODO show more info? Also HW info? } } else { //ReaderID check failed readerCheckFailCount++; if (readerCheckFailCount > 5) { //Reader Check failed multiple times if (m_ReaderFound) { //Previously found --> Asume Reader is lost! m_ReaderFound = false; SetUiEnabled(false, 0); } } System.Threading.Thread.Sleep(200); } } } } }