All the
code tips, tricks and hints are provided “As Is”
and there are no guarantees that it works. You have to test ,evaluate
and use these things at your own risk!
Also think
about that the storagecard doesn’t have unlimited write access!
When I
started developing software for small devices like PocketPC’s,
the applications were first of all very small and the database size
didn’t exceed 10 MB.
Also on
WinCE4 devices there was plenty of space for the database.
Now with
the time the applications have grown and WinMobile 5 appeared. The
things changed.
First of
all the Database size has grown dramatically also the available space
for file storage (e.g. the T-Mobile MDA PRO) decreased.
On some
devices there was only 10MB space for files left.
So I decided to
put the Database on the storagecard. After the first run I thought
“Great it works”.
But
first of all some strange errors occurred. For example an error said
“The command needs an open and prepared connection”
meanwhile in the same moment the state property of the connection
said the database were open and ready.
Time after
time I found out, that this problems only happens when the device
were turned off or was in some kind of sleep state.
Therefore I
found a nice class of the OpenNETCF (s. OpenNETCF.org) which gave me
the ability to react on the device open event.
So first of
I all I bind the event like this:
DeviceManagement.DeviceWake
+=
new
DeviceNotification(DeviceManagement_DeviceWake);
Then
I add some code which reopens the SQL Connection. But attention! I
experienced that the event doesn’t always fire IMMEDIATLY when
the device is turned on and often fire many times instead of exact
one time per turn on!
So
you have to handle that this method isn’t going to run twice
ore more times at once!
To
Handle the multiple DeviceWake events I write back the DateTime of
the DeviceWake events and I only execute code if some time (one
second) has passed till the last Wakeevent.
I
assume no one is quick enough to turn the PocketPC on and off again
in one second...
///
<summary>
///
Time
when the Devicewakeevent fired the last time
///
</summary>
DateTime
LastWake
= DateTime.Now;
///
<summary>
///
As
long as an Wakeevent is in progress this flag is true!
///
</summary>
public
bool
lWakeInProgress = false;
void
DeviceManagement_DeviceWake()
{
//Gettin
the Timespan till the last Wakeevent fired
TimeSpan
TimeLastWake = DateTime.Now.Subtract(this.LastWake);
//So
if theres no other Wake in Progress and the last wake event was
before one second or longer I run my wake code
if
(!this.lWakeInProgress &&
TimeLastWake.TotalSeconds > 1)
{
try
{
//Setting
a hint to see that a wake is in progress
this.lWakeInProgress
= true;
//Showing
the user that we do something
Program.SetCursor(true);
//Giving
the OS the change to handle own events ( this seemed to be important
du to incoming phone calls which could wake up the device)
Program.Sleep(2);
//Now
look if the Database physically exists, if not..
if
(!System.IO.File.Exists(Program.cDataBaseFile))
{
//
We’ll waiit 2 Seconds ( after this I never had any problems of
an missing database file
Program.Sleep(2);
}
//Now
I call my code to reconnect to database (closing and reponing the
conection)
Program.ReConnectToDataBase();
//Soll
das Wake protokolliert werden?
if
(Program.lLogWake)
{
Program.oLog.LogErr("DeviceWake()",
"Wake beendet");
}
//Wieder
aufbauen
Program.SetCursor(false);
this.lWakeInProgress
=
false;
}
catch
{
}
finally
{
this.lWakeInProgress
= false;
}
//Updating
the LastWake event DateTime ...
this.LastWake
= DateTime.Now;
//and
resetting the hint that the wake is in progress
this.lWakeInProgress
= false;
}
Some more
hints:
-
Check that SQL
Procedures triggered by timer or something similar have their on SQL
Connection otherwise you could get some errors concerning and
“already in use “ error of the SQL Connection
-
Be aware of using the
PowerDown event in combination with the DeviceWake event. Some times
the order of these 2 Events are mixed up ! So the PowerDown fires
after the DeviceWake.
-
The Code of the
PowerDown event isn’t always executed before the
device is really off . Furthermore it seems like the code is
put onto the stack and is executed when the device is turned on again
-
No code of your .NET
Environment is being executed while the device is off ( no timer, no
events, etc...) But there are API - Functions to set an wakeup time.
For this s. s. Daniel Moth’s Blog
-
All devices should have installed the Latest ROM and OS Version
because they improved a lot of handling of internal storages !
So I hope
this was some kind of help concerning database SQL Mobile on
PocketPC’s with, .NET CF2.0 and Storagecards
It this was
helpfull pls. Concider an paypal spent