In one of our Windows Mobile applications we've been noticing some bizarre exceptions where we're logging a rare exception of "Unspecified Error".
It's always a pain getting this kind of error, you really have no clue where to turn. Pin pointing the general area and finding the point at which the application fails tends to be the only route and led us to find it was database related.
Further research into the error implies that the issue lies in SQL CE (we're using SQL CE 3.5 SP1). It's at the point we're touching the database but ONLY after the device has come out of stand by mode and ONLY if the database file is located on the storage / memory card. Database files stored on the internal memory are fine.
It turns out that the SQL Server Mobile engine has a bug when the database file is accessed before the block device driver (that allows you to access the database file) has started up after the device is restored from suspend mode.
There's a hotfix available for previous versions but not the latest and it only seems to give a more intelligible error message of "The disk is not ready" or "The OS storage system (RAM, CF, SD, or IPSM) is not responding. Retry the operation".
Which leaves us with the choice of what to do in this instance.
1. Handle database access exceptions until the device is ready again.
2. Wire up to the device's power events and handle the Power up event.
3. Move the database file to the Internal memory.
I decided to go with option 2. A significant amount of work would have been required for option 1. The devices we're working against have a 2GB storage card versus a 64mb internal memory so I felt this wouldn't work for us without impacting performance so I didn't go for option 3.
The easiest way to go with option 2 is by using the opennetcf smart device framework framework which in the OpenNETCF.WindowsCE.PowerManagement namespace allows you to wire up to all the Power related states and their changes.
Here's one possible solution:
<span style="color: rgb(96, 96, 96);"> 1:</span> <span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 255);">void</span> Main()
<span style="color: rgb(96, 96, 96);"> 2:</span> {
<span style="color: rgb(96, 96, 96);"> 3:</span> OpenNETCF.WindowsCE.PowerManagement.PowerUp += <span style="color: rgb(0, 0, 255);">new</span>
OpenNETCF.WindowsCE.DeviceNotification(PowerManagement_PowerUp);
<span style="color: rgb(96, 96, 96);"> 4:</span> <span style="color: rgb(0, 128, 0);">//Run the app</span>
<span style="color: rgb(96, 96, 96);"> 5:</span> }
<span style="color: rgb(96, 96, 96);"> 6:</span>
<span style="color: rgb(96, 96, 96);"> 7:</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 255);">void</span> PowerManagement_PowerUp()
<span style="color: rgb(96, 96, 96);"> 8:</span> {
<span style="color: rgb(96, 96, 96);"> 9:</span> System.Windows.Forms.Application.Exit();
<span style="color: rgb(96, 96, 96);"> 10:</span> }
OpenNetCF has some great products, their download count is at 3/4 million at the time of writing. They have good customer service too.