Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug Button Click in Firefox intermittently causes ArgumentException
6 replies. Latest Post by fruhlig on November 4, 2009.
(0)
ccoombs
Contributor
5168 points
758 Posts
04-16-2009 9:54 AM |
My applications have been sending me error reports for firefox users, and I've finally been able to reproduce it and create a test case. The stack trace is:
System.ArgumentException: Value does not fall within the expected range. at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData) at MS.Internal.XcpImports.MethodPack(IntPtr objectPtr, String methodName, Object[] rawData) at MS.Internal.XcpImports.MouseEventArgs_GetPosition(MouseEventArgs args, UIElement relativeTo) at System.Windows.Input.MouseEventArgs.GetPosition(UIElement relativeTo) at System.Windows.Controls.Primitives.ButtonBase.OnMouseMove(MouseEventArgs e) at System.Windows.Controls.Control.OnMouseMove(Control ctrl, EventArgs e) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
Pretty simple test case:
Page.xaml
<UserControl x:Class="TestSilverlight.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <Canvas x:Name="LayoutRoot" Background="White"> </Canvas></UserControl>
Page.xaml.vb
Public Class Page : Inherits UserControl Private WithEvents btnTest As New Button() With { .Content = "Test" } Private WithEvents btnBreak As New Button() With { .Content = "Break" } Public Sub New() InitializeComponent() LayoutRoot.Children.Add(btnTest) btnBreak.SetValue(Canvas.LeftProperty, 50.0) End Sub Private Sub btnTest_Click() Handles btnTest.Click If Not LayoutRoot.Children.Contains(btnBreak) Then LayoutRoot.Children.Add(btnBreak) End Sub Private Sub btnBreak_Click() Handles btnBreak.Click LayoutRoot.Children.Remove(btnBreak) End SubEnd Class
In Firefox, click the 'Test' button, which adds the 'Break' button to layoutroot. Clicking the break button will sometimes cause the unhandled exception. It doesn't seem to happen to controls that remain on layoutroot.
It definitely has something to do with how you move the mouse cursor when you click the button. If you click the button 'nicely' (via spacebar press while it has focus, or slowly moving your mouse cursor over it and clicking it slowly), it doesn't break. It has to occur quickly, but it is most easily reproducable if the x,y position of your cursor on mousedown isn't the same as the x,y position of the cursor on mouseup.
I've seen posts that describe the same thing happening with other controls, and have been able to reproduce it in my live apps with a datepicker. The issue doesn't seem to happen in IE or Chrome. My apps are running SL2, but according to this post (http://silverlight.net/forums/p/85193/197950.aspx), it is still a problem in SL3B1.
My current workaround is to check the exception type in Application_UnhandledException. If the exception type is ArgumentException, and the exception.ToString matches exactly the exception I posted above, then ignore it. Hopefully someone has a more graceful workaround.
ld1453
Member
284 points
112 Posts
04-17-2009 11:46 AM |
Hi, I'm the poster from the thread you referenced in your post.
I'm sorry, in the previous thread I didn't mean to make it sound like the problem wasn't fixed per-se... just not completely fixed. Silverlight still dies in Beta 3 during debugging but not when the debugger isn't attached. I'm hopeful that it'll get better through the beta releases since the somebody somewhere is already aware of the problem.
I wish somebody with an [MSFT] after their name would chime in...
StefanWick
2864 points
438 Posts
04-18-2009 11:40 AM |
Hello,
you get the exception because there are mouse move events for the button in the queue that get processed after the you have removed the button from the tree.
We have a bug on this in our database and we working to get the root cause addressed for the final release of SL3.
Private
Thanks, Stefan Wick
04-20-2009 10:08 AM |
That's a pleasure to hear Mr. Wick.
I had gotten it reproduced a while back by having a button remove itself, add another, and then use ButtonAutomationPeer to 'click' the newly created button. I tried your work around with it and doesn't fix it but it may help under more normal usage.
Thanks so much for the explaination and effort.
public partial class Page : UserControl { int max_iterations = 500; int count = 0; public Page() { InitializeComponent(); Button b = new Button(); b.Content = "Click to Start Add/Remove Button Process"; b.Click += new RoutedEventHandler(Exception_Click); LayoutRoot.Children.Add(b); } void Exception_Click(object sender, RoutedEventArgs e) { if (count < max_iterations) { count++; Dispatcher.BeginInvoke(RemoveButton); //LayoutRoot.Children.Clear(); Button b = new Button(); b.Content = "Now Click and Move the mouse a couple times " + Environment.NewLine + "while buttons are being added & removed (" + count + " of " + max_iterations + " )"; b.Click += new RoutedEventHandler(Exception_Click); LayoutRoot.Children.Add(b); ButtonAutomationPeer peer = new ButtonAutomationPeer(b); IInvokeProvider provider = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider; provider.Invoke(); } else { //Stop adding/removing buttons count = 0; } } void RemoveButton() { LayoutRoot.Children.Clear(); } }
10-01-2009 4:00 PM |
I've run into this problem yet again in SL3 RTW and it is still not working. Has anybody found a work around?
Since it's a little hard to reproduce (but definitely happens!) by hand here's a repro that could help out: http://www.waywardscholar.com/ButtonValueExpectedRangeBug.zip
10-01-2009 4:14 PM |
yeah i really wish this would get fixed. i still have my suppression technique in place from my original post. here's the VB implementation, but its not much different for C#
Public Class App : Inherits Application
Private Const FIREFOX_BUG As String = _ "System.ArgumentException: Value does not fall within the expected range." & vbNewLine & _ " at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)" & vbNewLine & _ " at MS.Internal.XcpImports.MethodPack(IntPtr objectPtr, String methodName, Object[] rawData)" & vbNewLine & _ " at MS.Internal.XcpImports.MouseEventArgs_GetPosition(MouseEventArgs args, UIElement relativeTo)" & vbNewLine & _ " at System.Windows.Input.MouseEventArgs.GetPosition(UIElement relativeTo)" & vbNewLine & _ " at System.Windows.Controls.Primitives.ButtonBase.OnMouseMove(MouseEventArgs e)" & vbNewLine & _ " at System.Windows.Controls.Control.OnMouseMove(Control ctrl, EventArgs e)" & vbNewLine & _ " at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)"
Private Sub Application_UnhandledException(sender As object, e As ApplicationUnhandledExceptionEventArgs) Handles Me.UnhandledException If Diagnostics.Debugger.IsAttached Then Exit Sub e.Handled = True If TypeOf e.ExceptionObject Is ArgumentException Then If e.ExceptionObject.ToString() = FIREFOX_BUG Then Exit Sub End If
' your normal error handling technique here
End Sub
End Class
fruhlig
2 points
1 Posts
11-04-2009 5:44 PM |
It's a pity in SL3 it still produces the same exception.