Click here to Skip to main content
15,881,709 members
Articles / .NET
Tip/Trick

Expect the Unexpected

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
6 Apr 2011CPOL1 min read 19.3K   5   10
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:
C#
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

License

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


Written By
Software Developer (Senior) Paddedwall Software
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.

Comments and Discussions

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