Blond Mobile


Archive for the ‘Uncategorized’ Category

Windows Mobile: How to run a method when device wakes up/ comes back from sleep mode

Thursday, March 20th, 2008

Its very surprising that there is no wakeup event or member in the managed SystemState class that indicates that the device has returned from sleep mode.

Fortunately, we can use the CeRunAppAtEvent function from coredll.

public static extern bool CeRunAppAtEvent(string AppName,
            int WhichEvent);

Starting an application when the device wakes up, is very straight forward,  and all you need is to pass the path of the application to run (and also remember to remove it…)

Running a method is a bit more complicated; instead of the application path you would write 

"\\\\.\\Notifications\\NamedEvents\\eventName"

Where the eventName is a name of a native event. here is the code you will need to start a wakeup event, catch it and run your method

 private const int NOTIFICATION_EVENT_WAKEUP = 11;
 private const UInt32 INFINITE = 0xFFFFFFFF;

 [DllImport("coredll.dll", SetLastError = true)]
 public static extern bool CeRunAppAtEvent(string AppName,
     int WhichEvent);

 [DllImport("coredll.dll",SetLastError = true)]
 private static extern int WaitForSingleObject(IntPtr hHandle,
     UInt32 dwMilliseconds);

 [DllImport("coredll.dll", SetLastError = true)]
 private static extern IntPtr CreateEvent(IntPtr lpEventAttributes,
         bool bManualReset, bool bInitialState, string spName);

private static General.NoParamsFunctionHandler _eventToRunOnWakeup;

 public static General.NoParamsFunctionHandler EventToRunOnWakeup
 {
     set
     {
         if (_eventToRunOnWakeup == null)// so we only run one thread
             StartOnWakeUpService();
         _eventToRunOnWakeup = value;
     }
 }

 private static void StartOnWakeUpService()
 {
     try
     {
        CeRunAppAtEvent(
        "\\\\.\\Notifications\\NamedEvents\\NativeDeviceWakeUpEvent",
             NOTIFICATION_EVENT_WAKEUP);

      _wakeupServiceThread = new Thread(new ThreadStart(WakeupProc));
      _wakeupServiceThread.Start();
     }
     catch { }
 }

 private static void WakeupProc()
 {
     IntPtr hEvent = CreateEvent(IntPtr.Zero, false, false,
          "NativeDeviceWakeUpEvent");
     while (!_close)
     {
         WaitForSingleObject(hEvent, INFINITE);
         if (_eventToRunOnWakeup != null)
             _eventToRunOnWakeup();
     }
 }

since this thread will be running in the background, we want to make sure it will stop when we are done. hence the below method

public static void Close()
{
    _close = true;
    if(_wakeupServiceThread != null)
       _wakeupServiceThread.Abort();
}

Windows Mobile: How to measure execution time in C#

Wednesday, March 12th, 2008

I wanted to test a function’s duration (for various  cases).

I thought it to be simple and be as in . Net C#.

I wrote something like this:

DateTime start = DateTime.Now;
//function code......

DateTime end = DateTime.Now;
TimeSpan duration = end - start;

I got that the duration is 0!! I also tried using

duration.TotalMilliseconds; and DateTime.Now.Ticks;

but the ticks for the start and end were the same. this seemed very strange.

To solve the problem, instead of using  DateTime.Now I used

System.Environment.TickCount;

It seems that the time resolution for DateTime.Now depends on the operation system, and for .Net compact framework its  one Second!!!.

 

Windows Mobile UI: How to monitor the device’s screen orientation

Thursday, February 28th, 2008
getting screen orientation:

There are two members that indicate the screen orientation mode (Landscape or portrait)

SystemState.DisplayRotation

SystemSettings.ScreenOrientation

Note: these 2 members may not always be the same. Ill will discuss this issue in my next post

Monitoring screen orientation

When the screen orientation changes we have 2 ways to know about it:

System state change event

When the SystemState.DisplayRotation is changed, an event is fired. We can catch it and use it:

Creating the event:

private SystemState _orientationWatcher;
_orientationWatcher = new
               SystemState(SystemProperty.DisplayRotation);
_orientationWatcher.Changed += new
               ChangeEventHandler(OrientationWatcher_Changed);

Below is the function that "does something" when there is an orientation change. you should make sure to use the new orientation value from the args param and not get it from the System state since its read from the registry.

void OrientationWatcher_Changed(object sender, ChangeEventArgs args)
{
     int newOrientation = (int)args.NewValue; // new orientation
     //do what you want to do when there is a orientation change
}

resize event

You can use the control’s  resize event and check if the screens orientation was changed. you can use the SystemState or SystemSettings parameters.

The problem with this is that the resize event is called allot of times: its called around 4 times each time you display a screen, create a screen,  and resize the screen - i.e. change the screen’s orientation.

Hence I don’t recommend using it for monitoring the rotation change.

 

 

 

 

Jajah is the VoIP player that brought you web-activated telephony.