meds
meds

Reputation: 22926

symbol barcodescanner doesn't like being closed in a scan callback

The motorolla symbol barcodescanner works with a delegate event function, if I attempt to disable the scanner within the context of that space I get a null reference exception.

If I simply disbale the scanner but outside that functions execution everything works fine.

Any ideas how to get around this? The scan opens a new form which means the scanner needs to be disabled...

Upvotes: 1

Views: 783

Answers (4)

tcarvin
tcarvin

Reputation: 10855

I strongly suggest that you create your scanner object, or a wrapper around that object, as a singleton in your project. I've found the Dispose handling of data-collection objects in several vendors to be iffy, and rapid recreation of the objects even more so. By using a singleton, you avoid the whole issue. One create at startup, one dispose at program shutdown.

Upvotes: 0

Nikoli
Nikoli

Reputation: 167

You can trigger a timer in the scanner callback function. In the timer tick callback you then disable the device. Just keep in mind that if the scanner callback is hit again before the timer tick you will need to reset the timer. Failure to do so may result in lost data received from the scanner.

Upvotes: 2

user153923
user153923

Reputation:

Paul's idea looks interesting.

Another option would be to enable/disable the scanner based on what control is selected using that control's GotFocus and LostFocus events.

One event could wire up all of it.

private void Control_FocusChanged(object sender, EventArgs e) {
  ScannerEnabled = ((Control)sender).Focused;
}

(You have to code that ScannerEnabled variable, though.

Properties Window

Upvotes: 1

Paul Farry
Paul Farry

Reputation: 4768

is the enable of the scanner a property or do you actually need to turn it off?

In a project I did where the scanner I was using was not able to be turned off was to abstract their library with my own, and have an Enabled property on my library. Then inside my class I just kept the instance of the real scanner operational and if my Enabled property was turned off I didn't raise the event to the outside class.

Therefore you might be able to do something similar here.

void ScannerCallback(object sender, EventArgse)
{
    if (Enabled)
    { 
      OnBarcodeArrived(this, EventArgs.empty);
    }
}

Upvotes: 1

Related Questions