title: C# Threading and events for pin pad device
body: <p>I am new in C# and currently working on the backend code to support PIN pad. Basically, my code </p>
  
  <pre><code>OpenDevice() -&gt; RequestPIN() 
  -&gt; key in PIN on PIN PAD -&gt; GetResultPIN() 
  -&gt; ConsolePrintOutPIN() -&gt; Print keyed PIN on the Console 
  </code></pre>
  
  <p>I don't know how to write thread for this, so that once the "Enter key" is hit on the device after PIN, the system would automatically rolls to function <code>GetResultPIN()</code>. So, with my elementary knowledge, I wrote the following codes using <code>Console.ReadLine()</code> to separate each procedure:</p>
  
  <pre><code>    static void Main(string[] args)
      {
          // 1. Open PIN Pad device
          OpenDevice();
  
          Console.ReadLine();// to hold up from the previous procedure, it is *not* for input data purpose
  
          // 2. Request PIN from PIN Pad device.
          //    On the PIN Pad device, it reads:
          //    "Key in the PIN:     "
          RequestPIN();
  
          Console.ReadLine();// to hold up from the previous procedure, it is *not* for input data purpose
  
          // 3. get PIN from the device
          GetResultPIN();
  
          // 4. Print out on the Console 
          ConsolePrintOutPIN();
  
          Console.ReadLine();// to hold up from the previous procedure, it is *not* for input data purpose
      }
  </code></pre>
  
  <p><strong><em>Question:</em> Can anyone give me any suggestions on how to use threading/event/delegate that can avoid using <code>Console.ReadLine()</code>?</strong></p>
  
  <p>As commended above, <code>Console.ReadLine()</code> is used just to stop the procedure (sorry about my naivety of using it this way....) Once I use <code>Console.ReadLine()</code>,   between <code>RequestPIN()</code> and <code>GetResult()</code>, the system would at least wait for me to Key in the PIN from the PIN PAD (connected to the computer through USB, <strong>not</strong> from key board), and then I would hit any key on the keyboard to pass <code>Console.ReadLine()</code> and <code>GetResultPIN()</code> would be able to get my PIN number from PIN Pad.....the whole program works now, it is not customer ready, because it is very choppy, doesn't flow due to <code>Console.ReadLine()</code> I added.....</p>
  
  <p>So ideally, all the method would flow together. Once the device is opened, <code>RequestPIN()</code> should show on the PIN Pad screen asking for PIN number, some one can key in and hit enter on PIN Pad and it naturally flow to <code>GetResultPIN()</code> and read the result, and then it prints the PIN on the console...`</p>
  
  <p>or</p>
  
  <p>if the person doesn't key in PIN, the device would wait for 30s and directly goes to <code>GetResultPIN()</code> and print out "0000" on the Console</p>
  
  <p>I have looked up treading and delegate, but am not sure how to use them in this situation.... Thank you!</p>
  
  <p><strong><em>Reference:</strong> RequestPin() and GetResultPIN are listed down below:</em></p>
  
  <pre><code>mIPAD.requestPIN(waitTime, pinMsg, minLen, maxLen, tone, option, ",");
  //This function wraps device command 0x04.  
  //It directs the device to prompt the user to enter a PIN 
  //by displaying one of five predetermined messages and playing
  // a specified sound.  
  //The messages on the device’s screen look like the figures below.  
  //The event associated with this function is 
  //OnPINRequestCompleteEvent. 
  </code></pre>
  
  <p><em>waitTime: Time the device should wait for the user to begin PIN entry</em></p>
  
  <p><em>pinMsg: Message to display as a user prompt, such as "Enter PIN", "ReEnter PIN", "Verify PIN", etc</em></p>
  
  <p><em>minLen and maxLen: minimum length and maximum length of PIN</em></p>
  
  <p><em>tone: beeping tone option</em></p>
  
  <p><em>Option: Verify PIN, not Verify PIN,  ISO0 FOrmat, ISO3 Format</em></p>
  
  <p><em>Output would be: an integer, 0: Success, Non-Zero: Error</em></p>
  
  <pre><code>    public void GetResultPIN()
      {
          StringBuilder sb = new StringBuilder();
          sb.Append(mIPAD.pin.KSN); 
                 // Key Serial Number: 
                 //a given number from the device, unique for each device
          sb.Append("," + mIPAD.pin.EPB);
                 // EPB: encryption of PIN after Dubpt TripleDES,
                 // essentially, EPB is PIN
          sb.Append("," + mIPAD.getStatusCode());
                 //status code: Zero is good/done
                 //             None-Zero is Error
          sb.Append("\r\n");
          result = sb.ToString();
      }
  </code></pre>
  
  <p><em>Basically, the GetResultPIN() returns a string of random code, for example:
  <code>9A00030000047A2000AB,AD781711481B08A2,0</code> when PIN is successful. If the pin-input part is skipped, it would return <code>,,0</code>.</em></p>