All the
code tipps tricks and hints are providerer “As Is” and there are no garantuees
that it works. You have to test ,evaluate and use these things at your own
Risk!
Also think
about that Storagecard don’t have unlimited write accesses!
When I
started developping software for small devices like PocketPC’s the applications
were first of all very small and the databasesize didn’t exceed 10 MB.
Also on
WinCE4 devices there was plenty enough of space for the database.
Now with
the time the application growed and WinMobile 5 appaered , the things changed.
First of
all the Database size has grown dramatically also the aviable space for file
storage (e.g. the T-Mobile MDA PRO) decreased.
On some
devices t here 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 occured. 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 happen 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
abillity 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 ofte
n fire many times instead of exact one time per turn on!
So you have to
handle that this mehtod aren’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 conecrning 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 execute before the device is really off . Furthermore it seems like
the code is put onto the stakc and executet when the device is turned on again
-
No
code of your .NET Enviroment is beeing executed while de device is off ( no
timmer, no events, etc...) But there are APIfunctions 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