Why does setting DataEventEnabled = true cause an exception?

Locked Why does setting DataEventEnabled = true cause an exception?

  • Friday, October 08, 2010 1:46 AM
     
      Has Code

    I have noticed something odd that I don't quite understand. When I am using POS for .Net with a legacy scanner service object, when I get a barcode scanned event in order to receive a new event, you need to set the DataEventEnabled property to true. Otherwise you only get the first event, and not the second event.

    What is odd is that when I run under a debugger, I always get a PosControlException exception with the message "Failed to set property DataEventEnabled.". This never happens the first time you turn this on when you create the scanner object, but always in the event handler.

    When I connect to the scanner directly to the OCX service object (not going through POS for .Net) I don't get this.

    At first I thought I was doing something wrong, as the sample app in POS for .Net works just fine. Until I realized it ALSO has the same problem! And that in the sample code, they have an empty try/catch block around setting that value to ignore the exception:

    try
    {
      dataEventEnabled.SetValue(posCommon, true, null);
    } 
    catch { }
    

    I can easily do the same in my code, which is what I have done below, but it bugs me that I get this exception. Is there any reason this exception occurs?

    void scanner_DataEvent(
      object sender, 
      DataEventArgs e)
    {
      // Find who sent the message
      Scanner scanner = sender as Scanner;
    
      // Process the barcode
      MainWindow_ProcessBarCode(encoding.GetString(scanner.ScanDataLabel));
    
      // Enable the data event for the next barcode
      try {
        scanner.DataEventEnabled = true;
      } catch { }
    }
    
    

All Replies

  • Sunday, October 10, 2010 6:46 PM
     
     

    Nobody? I found this thread from a while back, and I wonder if this guy was running into the same issue:

    http://social.msdn.microsoft.com/Forums/en-US/posfordotnet/thread/8a5ed3b5-7f41-4fd7-ba32-f53631cc7870

  • Monday, October 11, 2010 9:03 PM
     
      Has Code

    I have noticed something odd that I don't quite understand. When I am using POS for .Net with a legacy scanner service object, when I get a barcode scanned event in order to receive a new event, you need to set the DataEventEnabled property to true. Otherwise you only get the first event, and not the second event.

    What is odd is that when I run under a debugger, I always get a PosControlException exception with the message "Failed to set property DataEventEnabled.". This never happens the first time you turn this on when you create the scanner object, but always in the event handler.

    When I connect to the scanner directly to the OCX service object (not going through POS for .Net) I don't get this.

    At first I thought I was doing something wrong, as the sample app in POS for .Net works just fine. Until I realized it ALSO has the same problem! And that in the sample code, they have an empty try/catch block around setting that value to ignore the exception:

     

    try
    {
     dataEventEnabled.SetValue(posCommon, true, null);
    } 
    catch { }
    

     

    I can easily do the same in my code, which is what I have done below, but it bugs me that I get this exception. Is there any reason this exception occurs?

     

    void scanner_DataEvent(
     object sender, 
     DataEventArgs e)
    {
     // Find who sent the message
     Scanner scanner = sender as Scanner;
    
     // Process the barcode
     MainWindow_ProcessBarCode(encoding.GetString(scanner.ScanDataLabel));
    
     // Enable the data event for the next barcode
     try {
      scanner.DataEventEnabled = true;
     } catch { }
    }
    
    

     

    I wrote some code for dealing with using scanners in my own application, and I cant remember hitting this exception, so I will check in teh morning!

    But yes you need to reenable the data event after each scan...basically stops 2 quick scans that you werent perhaps ready for, so teh first scan calls the event, disables it, and once your finished you set it to true to allow for the next scan.


    Developer
  • Monday, October 11, 2010 11:13 PM
     
     

    If you don't have the event enabled, the scans still actually buffer up so as soon as you enable the event again, you will get the next event if there are barcode scans that have buffered up.

    You should not need to put an exception handler around that code, and with the Microsoft sample SO's you don't. But I think the guys writing the sample app ran into the exact exception I ran into with certain scanners so they wrapped it with an exception handler. Which means if you don't have it, and use a Symbol scanner, your app will crash :(. Not nice.

  • Thursday, October 14, 2010 10:02 AM
     
      Has Code

    If you don't have the event enabled, the scans still actually buffer up so as soon as you enable the event again, you will get the next event if there are barcode scans that have buffered up.

    You should not need to put an exception handler around that code, and with the Microsoft sample SO's you don't. But I think the guys writing the sample app ran into the exact exception I ran into with certain scanners so they wrapped it with an exception handler. Which means if you don't have it, and use a Symbol scanner, your app will crash :(. Not nice.

    I have checked in my code, I do not wrap around a try catch block , and I get no exception, so I really do not know what is wrong in your case.

    I am using the Gac Avery scanners. My code doesnt do anything special

    /// <summary>
        /// Enables the scanner if there is one available and it isnt already enabled
        /// </summary>
        public static void EnableScanner()
        {
          if (PosHardwareManager.Scanner != null)
          {
            if (!PosHardwareManager.Scanner.DeviceEnabled)
            {
              // Activate the scanner
              PosHardwareManager.Scanner.DeviceEnabled = true;
            }
    
            if (!PosHardwareManager.Scanner.DataEventEnabled)
            {
              // Allow data events
              PosHardwareManager.Scanner.DataEventEnabled = true;
            }
          }
        }
    


    Developer
  • Thursday, October 14, 2010 5:24 PM
     
     

    It never crashes in the enable code, only in the event handler when you try to turn it back on again. Unless you are calling the EnableScanner() function from your event handler?

    Either way I am sure it does not affect all scanners, but if you have access to a Symbol LS2208 scanner, I am curious if it would do the same thing for you as it does for me :)

  • Monday, October 18, 2010 3:43 PM
     
      Has Code

    yes my enable event is called from within the event handler, after it has finished dealing with the scan. I also need to do an invoke on the method as it wasn't a UI thread that was calling the method.  So perhaps you have your scanner object as a member of the form? This may cause issues when trying to alter any of its properties. The way I got round this was the following:

    private void Scanner_DataEvent(object sender, Microsoft.PointOfService.DataEventArgs e)
        {
          // as this will be on a different thread, on the first call, re invoke this method from the main creation thread
          if (this.InvokeRequired)
          {
            // invoke this method off of the creation thread
            this.BeginInvoke(new MethodInvoker(delegate() { Scanner_DataEvent(sender, e); }));
            return;
          }
          else
          {
            // any code here you wish to run...along with enabling the scan again
    
          }
        }
    

    Sorry if this does not relate to anything you have coded, but thought it may be worth mentioning...


    Developer
  • Monday, October 18, 2010 5:09 PM
     
     Proposed

    The scanner event should always be called in the context of the UI thread by POS.net provided you pass in a synchronization object to the PosExplorer when you create it. There is a parameter to the PosExplorer class that you can pass in, which should be a this pointer to your UI class. If you initialize your PosExplorer this way, then it will do the invoke for you if it is called on a different thread, so your event handler will be on the correct thread. The MSDN docs mention this, so you might want to try that in your code and it should get rid of the need to invoke the method on the UI thread:

    http://msdn.microsoft.com/en-us/library/ms829341%28v=WinEmbedded.11%29.aspx

  • Tuesday, October 19, 2010 10:58 AM
     
     
    thanks for the info, I did however come across that. I seem to remember deciding against it, as I have my whole hardware layer abstracted away from any winfowms projects in its own hardware abstraction layer. Perhaps I could still have found a way to incorporate it, but the way I have it just now, I can use this same layer for anything, whether it be winforms, wpf, console, or anything else with no changes.
    Developer
  • Tuesday, January 17, 2012 4:15 PM
     
      Has Code

    I found that this prevents the exception (reading the value first)

     

     

     bool s=activeScanner.DataEventEnabled;
    
      activeScanner.DataEventEnabled = true;

     

     


    • Edited by ParhsG Tuesday, January 17, 2012 4:15 PM
    •  
  • Tuesday, January 17, 2012 8:16 PM
     
     
    Nice catch! That explains why my code crashed, and Paul's did not. Since he always read the flag before he wrote it, which cleared the exception. It is still a bug somewhere in POS.net, but at least we know why my code crashed and his did not :)
  • Friday, January 27, 2012 6:25 AM
     
     
    thanks for the info, I did however come across that. I seem to remember deciding against it, as I have my whole hardware layer abstracted away from any winfowms projects in its own hardware abstraction layer. Perhaps I could still have found a way to incorporate it, but the way I have it just now, I can use this same layer for anything, whether it be winforms, wpf, console, or anything else with no changes.
    reputation managemant