static async System.Threading.Tasks.Task MainAsync(string[] args) { Console.WriteLine(".NETCore Console"); Console.WriteLine("SampleAsync_C#"); Console.WriteLine("--------------------"); Console.WriteLine("Library Version: " + iIDReaderLibrary.Version.LibraryVersion); //Get DocInterfaceControl instance DocInterfaceControl docIntControl = await Console_InitializeDocInterfaceControlAsync(); 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 = await docIntControl.ReadReaderIDAsync(); if (readerID != null) { Console.WriteLine(""); Console.WriteLine("Detected Reader:"); Console.WriteLine(readerID.ToString()); break; } } //Reader info obtained --> execute functions using menu Console.WriteLine(""); while (await Console_ExecuteAndContinueAsync(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(); } m_Completed = true; }
private static async System.Threading.Tasks.Task Console_Execute_ReadReaderIDAsync(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 = await _docIntControl.ReadReaderIDAsync(); 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 async void Worker_DoWorkAsync(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 = await m_DocInterface.ReadReaderIDAsync(); 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); } } } } }