Click here to Skip to main content
12,548,514 members (49,909 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked

Expect the Unexpected

, 6 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Never assume you know what the framework is going to do
I was working on our Silverlight4 app today, coding up a child window. I created the child window from a given page, add an event handler for the Closing event, and opened the window. I tested in the debugger.

Once I was done in the window, I clicked the OK button. Imagine my surprise when the string I was trying to retrieve was empty. I had tested the window under the debuger, and had already verified that it was setting the string property, but the parent page was getting back an empty string. Enter, the Debug.WriteLine() method.

What I found was shocking. It turns out that the OK button handler was indeed being hit, but before the method exited, the window's Closed and Closing events were firing - BEFORE the OK handler was done with its processing.

The problem was that I had placed the following code at the TOP of the OK click handler:
this.DialogResult = true;

When I moved it to the bottom of the click handler (after all of the processing was done), the events fired in the expected order and at the expected time.

The tip is never make assumptions about how the framework works. I can't recall this being an issue in Windows Forms apps, but it certainly made itself known while I was working in Silverlight.

Be careful out there.

EDIT =============

Changed tags to be more appropriate


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 Hmm... I may need to check this out ... Pin
Gary Wheeler12-Apr-11 0:24
memberGary Wheeler12-Apr-11 0:24 
GeneralI'd say : always expect the framework to work as the documen... Pin
Remi BOURGAREL11-Apr-11 21:05
memberRemi BOURGAREL11-Apr-11 21:05 
GeneralRe: The dpocumentation isn't always a) forthcoming, b) accurate,... Pin
John Simmons / outlaw programmer10-Nov-11 2:25
mvpJohn Simmons / outlaw programmer10-Nov-11 2:25 
GeneralIt is as John said. you never know what the framework is doi... Pin
stadelma11-Apr-11 20:50
memberstadelma11-Apr-11 20:50 
GeneralReason for my vote of 1 please refer MSDN http://msdn.micro... Pin
pravin dingore11-Apr-11 18:01
memberpravin dingore11-Apr-11 18:01 
GeneralRe: Your vote of 1 is undeserved. If you'll actually take time t... Pin
John Simmons / outlaw programmer10-Nov-11 2:32
mvpJohn Simmons / outlaw programmer10-Nov-11 2:32 
Generali was working with .NET from framework version 1.1 as far a... Pin
pravin dingore11-Apr-11 17:59
memberpravin dingore11-Apr-11 17:59 
GeneralReason for my vote of 5 Good to know, John! Pin
Johnny J.7-Apr-11 22:09
memberJohnny J.7-Apr-11 22:09 
GeneralReason for my vote of 5 Good catch! Pin
Wonde Tadesse5-Apr-11 11:09
memberWonde Tadesse5-Apr-11 11:09 
GeneralStandard behavior...albeit unexpected.... Pin
Andrew Rissing5-Apr-11 12:49
memberAndrew Rissing5-Apr-11 12:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 6 Apr 2011
Article Copyright 2011 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid