I have code like this:
// Create event handler delegate
symbolReader.ReadNotify += new EventHandler(symbolReader_ReadNotify);
When a barcode is scanned on my handheld device then symbolReader_ReadNotify is called.
This is a simplified version of that method:
/// <summary>
/// Event that fires when a Symbol scanner has performed a scan.
/// </summary>
private void symbolReader_ReadNotify(object sender, EventArgs e)
{
ReaderData readerData = symbolReader.GetNextReaderData();
// If it is a successful scan (as opposed to a failed one)
if (readerData.Result == Results.SUCCESS)
{
// Setup the next scan (because we may need the scanner
// in the OnBarcodeScan event below
Start();
// Get the handle of the window that the user was on when the scan was sent.
IntPtr handle = CoreDLL.GetTopWindow();
// If we have a barcode scanner method for this window then call that delegate now.
if (_scanDelegates.ContainsKey(handle))
{
Action<BarcodeScannerEventArgs> scanDelegate;
// Get the method to call for this handle
// (previously added to the _scanDelegates dictionary)
bool delegateRetrieved = _scanDelegates.TryGetValue(handle, out scanDelegate);
if (delegateRetrieved && (scanDelegate != null))
scanDelegate(e);
}
}
}
That works fine most of the time. But when the call to scanDelegate opens a new window that also needs to accept scans the event (symbolReader.ReadNotify) does not fire (when the scan is done on that window). But once the window closes (and scanDelegate(e) returns) the event does fire (but now I route it to the wrong window.
Is there someway to tell the app to send the event? Does it work like windows messages (i.e. there is a way to flush the messages) or is it just the Symbol library that is failing to send the event until it is too late?)
The one thing I have tried is calling Application.DoEvents in a loop in the window that is opened. But that does not seem to work.
Note: This is a Compact Framework app, but I don’t think this is a Compact Framework issue, so I am not tagging it with Compact Framework.
Any advice to get the event to fire when the scan happens (like it does when it is not a nested scan) would be great!
Does the scanDelegate(e) open a the new window as a Dialog? If so it’ll block the event from raising again untill it’s closed because it’s called (opened) from within the same eventhandler.
You can work around it by either delaying the opening untill after the event is handled, not opening it as a dialog or by opening it on a new thread (or use begininvoke on the delegate)