1

Just got a very mysterious error while coding a WPF-application in C#.

I've got an event in a custom User-Control, that shows a message box every time the text on a button is changed. When I edit the text on the button in XAML, the event fires - even without my application actually running.

When I tried to open a new WPF-window in the event, the app crashes with "Stackoverflow" - after that, VS (C# Express) crashed too and I wasn't able to open my project until I changed the event and deleted all content in my Debug and Release folders.

Why is this event triggered?

Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
Jazzschmidt
  • 989
  • 12
  • 27
  • Your app must have been running, as in the process was active, even if no windows were visible. I suspect that the debugger was still attached and the process was running. – Ben Robinson Dec 08 '11 at 10:11
  • I also thought so, but it doesn't require to start the app. After a reboot the event still fires. I opened VS, didn't compile it - instead I just changed the text on the button and the messagebox appeard. – Jazzschmidt Dec 08 '11 at 10:22
  • Well per the answers below I am clearly wrong, you learn something new every day. – Ben Robinson Dec 08 '11 at 10:24

2 Answers2

4

A usercontrol runs inside Visual Studio.

Basically Visual Studio loads the assembly containing the control and executes the control.

This means that if your control shows a message box for some event that is executed at design time, then that message box will be shown in Visual Studio as well.

To fix this, make sure your control doesn't execute harmful or similar code when hosted in Visual Studio.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • I figured this would be the reason, but is this actually the case? Then when exactly does it trigger? Every letter you add/remove? Every save? After you 'complete' editing the field? – Steven Jeuris Dec 08 '11 at 10:22
  • 1
    In case you can't change the code you could also check if your Control runs in Design Mode, i.e. in VisualStudio and prevent the code from being executed. See http://stackoverflow.com/questions/834283/is-there-a-way-to-check-if-wpf-is-currently-executing-in-design-mode-or-not – SvenG Dec 08 '11 at 10:22
  • Didn't know there's code executed in Design Mode. Thanks for your help! – Jazzschmidt Dec 08 '11 at 10:32
0

Some code can still run InDesignMode is your xaml bound to a DataProvider?

Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84