WPF Tooltip ArgumentOutOfRangeException not Fixed.

shy akocat 21 Reputation points
2023-08-07T22:56:29.5166667+00:00

I'm using WPF with .net framework 4.8/4.8.1.

After I create a SharpDX DX9 device, which maybe change the floating-point control word, so that every tooltip when mouse hover, program throw an ArgumentOutOfRangeException exception.

Similiar problem: https://stackoverflow.com/questions/74041753/why-is-this-argumentoutofrangeexception-thrown#comment131042797_74203377.

It indicates that Microsoft have fix the bug: https://devblogs.microsoft.com/dotnet/dotnet-framework-october-2022-security-and-quality-rollup/.

However I still encounter the problem. My OS is Windows 11 22H2 22621.2070.

WPF with .net 7 have no such problem, but I have other reason to use .net framework(such as DllExport not support .net core and ...).

Developer technologies Windows Presentation Foundation
Developer technologies C#
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-08-09T08:34:33.0866667+00:00

    Hi,@shy akocat.

    I reproduced your problem, the code reported an error under .NET Framework4.8, but it worked fine under .NET 7. (Windows 10 22H2)

    It Seems that your code is rigging the creation of a Direct3D object and a Device object, which might be affecting the underlying graphics context and causing a conflict with the DispatcherTimer.

    Here's a workaround to accomplish the same thing in .NET Framework. If you'd like to use the original code, you can file it on Developer Community.

    You could try wrapping your DevilCode() function call within a separate thread to ensure it doesn't interfere with the UI and the DispatcherTimer.

     public MainWindow()
            {
                InitializeComponent();
                Loaded += MainWindow_Loaded; // Subscribe to the Loaded event
            }
    
            private void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                // Call DevilCode function in a separate thread
                Task.Run(() => DevilCode());
            }
    
            public void DevilCode()
            {
                var d3d = new Direct3D();
                var device = new Device(
                    d3d,
                    0,
                    DeviceType.Hardware,
                    IntPtr.Zero,
                    CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded,
                    new PresentParameters(100, 100, Format.A8R8G8B8, 2, MultisampleType.None, 0, SwapEffect.Discard, IntPtr.Zero, windowed: true, enableAutoDepthStencil: true, Format.D24X8, PresentFlags.None, 0, PresentInterval.Immediate));
            }
    
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Andreas Leeb 50 Reputation points
    2023-11-10T07:29:11.59+00:00

    Hi,

    we have encountered the same problem and can't easily move the Direct3D device from the UI thread.

    I found that setting the tooltip duration to the next smaller value representable by the 24 bit floating-point precision (set by Direct3D) solves this issue.

    ToolTipService.ShowDurationProperty.OverrideMetadata(typeof(DependencyObject),
    	new FrameworkPropertyMetadata(int.MaxValue - 127));
    
    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.