Search code examples
inputbarcodecharacter

Detecting when a barcode scanner ends input


I am writing an app that reads the input from a barcode scanner, the way I want it to work like is: it works as a keylogger and when the input is fast enough I know I'm getting input from the barcode scanner.

Now my problem is I don't know when I get the last character from the barcode scanner so I know I have a final and valid barcode.

My method only gets called when a character is received, so this is when I have to check if my character is the final one received.

The way I wanted to do this is, I create a new thread at the end of my method that sleeps for MAXIMUM_TIME_BETWEEN_CHARACTERS_FROM_BARCODE_SCANNER + 1 milliseoconds, after it wakes up it verifies if I received any new characters, if I did it the thread dies, if no new characters are received it means the full barcode is received and I can use it.

But by doing this I started getting all sorts of weird bugs, like execution dying prematurely.

Can anyone suggest a different solution?

I am writing this in C# if it's relevant.


Solution

  • I restructured this solution:

    The way I wanted to do this is, I create a new thread at the end of my method that sleeps for MAXIMUM_TIME_BETWEEN_CHARACTERS_FROM_BARCODE_SCANNER + 1 milliseoconds, after it wakes up it verifies if I received any new characters, if I did it the thread dies, if no new characters are received it means the full barcode is received and I can use it.

    and it eventually worked. I still do not know what caused my previous errors but after restructuring the code I have had no problems with this approach at all.