4

On my WPF app (.net 4.0) on a device without physical mouse (only touchscreen) a user as selected a value in a Combobox. After 6 minutes of "inactivity" (no action from the user or in the soft) a exception has been raised:

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Input.StylusDevice.GetMouseButtonState(MouseButton mouseButton, MouseDevice mouseDevice)
at System.Windows.Controls.ComboBox.OnAutoScrollTimeout(Object sender, EventArgs e)
at System.Windows.Threading.DispatcherTimer.FireTick(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

It seems after user explanation to reproduce the bug this exception appeared when an usb keyboard had been connected to the device.

I'm unable to reproduce the bug. Search on google didn't give me any leads

I've seen on the decompiled 4.0 framework code (I do not find 4.0 downloadable sources) and source browser (framework 4.6) here some leads:

   private void OnAutoScrollTimeout(object sender, EventArgs e)
    {
      if (Mouse.LeftButton != MouseButtonState.Pressed || !this.HasMouseEnteredItemsHost)
        return;
      this.DoAutoScroll(this.HighlightedInfo);
    }

The issue seems to come from :

 public MouseButtonState LeftButton
    {
        get
        {
            return GetButtonState(MouseButton.Left);
        }
    }

    protected MouseButtonState GetButtonState(MouseButton mouseButton)
    {
        // 
        if ( _stylusDevice != null && _stylusDevice.IsValid) 
            return _stylusDevice.GetMouseButtonState(mouseButton, this);
        else
            return GetButtonStateFromSystem(mouseButton);
    }

    [SecurityCritical, SecurityTreatAsSafe]
    internal MouseButtonState GetMouseButtonState(MouseButton mouseButton, MouseDevice mouseDevice)
    {
        if (mouseButton == MouseButton.Left)
        {
            return _stylusLogic.GetMouseLeftOrRightButtonState(true);
        }
        if (mouseButton == MouseButton.Right)
        {
            return _stylusLogic.GetMouseLeftOrRightButtonState(false);
        }

        //       can defer back to the mouse device that called you and it will call Win32
        return mouseDevice.GetButtonStateFromSystem(mouseButton);
    }

It's clearly possible the code has changed between framework release but even if some changes could impact this code, I do not see where a null reference exception can occurs.

Can anybody help me ?

Thanks

UPDATE: After more investigation I've found a part of the answer. While connecting the keyboard on the USB, it impacts the touch screen which is on the same hardware Usb internal hub. It seems windows is lost at this moment and the management of the "mouse" is impacted...

Tchoupi
  • 303
  • 3
  • 13
  • 2
    check for mouseDevice or mouseButton null – Vivek Nuna Nov 04 '16 at 14:08
  • Unfortunately this is not my code but the framework itself. and this code is executed automatically on the following combox.OnAutoScrollTimeout The only way I see is to override the combobox to change this method... but I think this just hide an issue which can probably appears elsewhere – Tchoupi Nov 04 '16 at 14:24
  • 1
    `StylusDevice` has many unanswered quaestion with `ObjectReferenceNotSetException`, so I think that this is probably a bug in WPF, and you have to create workaround. – VMAtm Nov 04 '16 at 16:18
  • damnit :'( thanks for the answer, the only way I currently see is to override controls implementations ... seems to be a lot of work to check each controls for this ... – Tchoupi Nov 04 '16 at 16:30

0 Answers0