A client of mine who builds Windows Mobile applications for the medical industry, contacted me to help him track down a hard-to-find application bug.
The symptom of the problem was that the app would occasionally encounter a ObjectDisposedException ... the error occurred on only one client's device, was not encountered consistently, and was not reproducible in our development environment. (just the kind'a bug we all love)
We knew the problem related to an area of the application where the user moves back-and-forth (in a variety of ways) between 2 non-modal forms but nothing more specific. After spending time reading through the code and my client working with his client to get a better understanding we finally identified the problem ...
one could say ... X marked the spot ...
There's an application out there that I wasn't aware of called "X-Button". When installed onto a device, the X-Button application allows you to change the behavior of the "X" that appears in the top-right corner of top-level and non-modal forms.
Normally the "X" in the corner of a form minimizes the current form. Many of us, me included, feel that the decision for Windows Mobile to use an "X" to minimize a form - rather than the more obvious action of exit - was a bad idea. But that's how things are and that's behavior that developers expect.
When you run the X-Button application, it gives you the option to End programs by tapping "X". In other words, they attempt to change the "X" from a minimize button to an exit button. Well that's not exactly true...
The X-Button application changes the X button to send a Close message to the current form. If that form is a top-level form, the form's application does exit. However, if the form is a non-modal child of another form, the form simply closes which is exactly what happened in my client's case.
What was happening was that the user, having enabled this X-Button feature, would occasionally tap the non-modal form's X to switch to another application - this closed and therefore disposed the current form. An action that was not possible on this form under the normal behavior of Windows Mobile.
As a closed form, the underlying Windows object is gone but the .NET Form class still exists. The next time the user selected an option that tried to show this form, the system returned an ObjectDisposedException.
My point in this long story is that you'll want to program defensively around this sort of problem. The X-Button program is being included by default on a number of HTC manufactured devices and is included in many of the HTC ROM upgrades. HTC is a huge manufacturer of Windows Mobile devices so lot's of users will have this application.
To defend your applications against this issue, 4 things come to mind
- Use modal forms as much as possible, I find that modal forms give an application a much more predictable flow. It may take some forethought to be sure that modal forms don't interfere with the user's workflow but I think it's worth the effort.
- You can accept the users intent to exit the application and override your non-modal forms' Closed event to shutdown the application.
- You can force the X to always behave as a minimize button. Override the modal forms' Closing event, set e.Cancel to True, and then programmatically minimize the form.
- At a minimum, you'll want to safely close the form which means either null-ing the reference to the form or setting a flag that indicates that the form must be recreated before it can be shown again (or perform other operations that require the underlying window).
I now include the use of X-Button in my application testing. I encourage you to do the same.
If you have a newer HTC device you may already have it. I didn't have an HTC devices that included the program so I found a copy at this discussion group.
I tried to install the X-Button program on one of the emulator images but it didn't work. It however, works fine when installed onto one of my older HTC devices.
Posted
Oct 16 2007, 09:20 AM
by
jim-wilson