NullPointerException when enabling/disabling scanner from barcode data input handler

433 views Asked by At

I'm developing for a Motorola barcode scanner and it seems I'm having some threading issue. When I set barcode_OnRead() as a handler for input from the scanner and manipulate the state of the scanner in the same flow, I get a NullPointerException soon after. For instance this will cause the issue:

private void barcode_OnRead(object sender, Symbol.Barcode.ReaderData readerData)
{
  this.barcode.EnableScanner = false;
}

It seems to me like a threading issue, but I'm not very proficient in C# and I don't know how to run sth on a different thread (the UI thread for instance)?

Edit: The code above alone will trigger the problem. this.barcode is not null at the time. The stack trace from the exception:

at Barcode.Barcode.SetScanParameters()
at Barcode.Barcode.Reader_ReadNotify(Object sender, EventArgs e)
at System.Windows.Forms.Control.TASK.Invoke()
at System.Windows.Forms.Control._InvokeAll()
at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at AppName.Program.Main()
0

There are 0 answers