Blond Mobile


Archive for the ‘Windows Mobile’ Category

Windows Mobile: Memory leaks and monitoring performance Tool (RPM)

Tuesday, August 19th, 2008

As most of you know, JAJAH released a pre-installed Windows Mobile VoIP client for the Japanese EMobile operator.

While working on it, I checked for performance issues and memory leaks. This was critical since in addition to outgoing VoIP calls the application runs in the background and listens to incoming VoIP calls. In order to monitor the performance I used the .Net compact framework remote performance Monitor (RPM). this tool is easy to use, once you understand how to install and use it, and this is the reason for this post.

Installation:

copy netcfrtl.dll and netcflaunch.exe  from C:\ProgramFiles\Microsoft.NET\SDK\ CompactFramework\ v2.0\ WindowsCE\wce500\armv4i to the Windows directory on your device. (to copy from your desktop to your windows mobile device use ActiveSync)

Running the PRM:

Connect your device to the desktop using ActiveSync

Open NetCFRPM.exe from C:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\bin

Select "File" and then "Live Counters"

image 

In the device box select "ActiveSync"  and in the "application" box enter your application you wish to test. Press "connect". the application will start and you will see the changing counters.

for more details about the counters check out David Kline’s post

image image

Using this view you can see the counters change and see what’s going on. to make things easier to monitor, you can have a view of the heap at real time and compare the GC heap at different intervals.

imageGC Heap View:

Click "View GC Heap" to view the heap status. In this view you can see the number of instances  each object has and the memory it consommes. you may also see the root object that is keeping the object alive, i.e. what is the object that holds a reference to the object and hence its not garbage collected.

 

 

 

imageComparing heap states

Its very helpfully comparing different heap states over time. This can help you see the difference in object over time and help you find leaks in various states of your application. When interested to know the heap state click "View GC Heap". when you want to compare the heaps memory states go to "View" and then select "Compare Heap". this view allows you to view the difference in object during the run time.

Windows Mobile: How to take screenshots of your device or emulator

Wednesday, July 16th, 2008

In order to take a screen shot of your handset or of the emulator you should do the following:

1- Open the “Remote Zoom In” application from the “Visual studio Remote Tools”

WM screenshot

2- Select to what device or emulator you want to connect. In case your connecting to a device make sure its connected to the PC with the USB cable.

windows mobile screenshot

3- the application will connect to the device/emulator  and you will see the screenshot. if you would like to take another screenshot, just click the “Refresh” button

4 - To save the screenshot, select “Save as”.

Windows Mobile, C#: How to stop a thread after calling CeRunAppAtEvent / WaitForSingleObject

Thursday, April 17th, 2008

In a previous post, I wrote about how to run a method when the device wakes up, or comes back from sleep mode. In that post I wrote about calling the CeRunAppAtEvent native function, which signals a given  event (hEvent) when the device wakes up.  I used the WaitForSingleObject(hEvent, INFINITE) function to wait until the hEvent is signaled.

The problem occurs when wanting to close the application, and a thread, which called the WaitForSingleObject()  is still waiting for the event. In that post, I wrote that in case wanting to close the application the below function should be called in order to terminate the waiting thread.

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

Unfortunately, The wakeupServiceThread could not be aborted, since it was waiting due to the call to WaitForSingleObject function.

The solution was to stop waiting on the WaitForSingleObject function,  and for that the _nativeWakupEvent event needed to be signaled.

Here is the code:

[DllImport("coredll.dll", SetLastError = true)]
        public static extern bool EventModify(IntPtr hEvent,
            [In, MarshalAs(UnmanagedType.U4)] int dEvent);
private const int NATIVE_EVENT_SET = 3;

public static void Close()
{
     _close = true;
     if (_hEvent != IntPtr.Zero)
             SetEvent(_hEvent);
}
private static bool SetEvent(IntPtr hEvent)
{
     return EventModify(hEvent, NATIVE_EVENT_SET);
}

Windows Mobile,C#: How to minimize your application/Send it to the background

Friday, April 11th, 2008

I wanted to send my application to the background. Simple enough, I just wrote Form.Hide() (or Form.visible = false). this did the job, my application was minimized and running at the background. At some point I wanted to use my application again, I clicked the applications icon, but nothing happend, I went to the running programs list in the settings, but my application was not there! my application was running in the background, and there was no way for me to close or access it!

Lucky enough, I used the Remote Process Viewer to find my application and kill it.

It seems that when Hiding a form (form.Hide()), The form is hidden from the user, and not only minimized. Funny that when clicking the “x” (which for some reason is called smart minimize), the application is minimized and sent to the background, where as using the hide() or visible=false, the application is hidden from the user and is not accessible.

The  Form class has a property which is called WindowsState. This property indicates the visual state of the form. unfortunately, the states that Windows Mobile/ .Net compact framework supports are ‘Normal’ and ‘Maximized’ and there is no support for the ‘Minimized’ state.

The solution for this problem is to call the Native function ShowWindow() with the ‘Minimized’ property:

[DllImport("coredll.dll")]
        static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

        const int SW_MINIMIZED = 6;

        public void Hide()
        {
            ShowWindow(_form.Handle, SW_MINIMIZED);
        }

Hiding the application this way, will not “hide” the application from the user, but only minimize it and the application will be  “reachable” from the running programs list.

Windows Mobile: How to view processes on a Windows Mobile Device or Emulator

Wednesday, April 9th, 2008

In a windows mobile device you can only view the running applications, and this is only if the application has a form. This means that you cannot view various processes that are running on a WM device, like you would be able to do on you PC, or Laptop using the “Task Manager’. fortunately, Visual Studio, has a solution for this problem:

Go to Visual Studio ->Remote Tools and select Remote Process Viewer

visual studio remote process viewer

Select the device which you would like to see its processes.

image

you will get a window, with all process that are running on your Windows Mobile device. In this window you will be able to view the processes and kill them, just like you would be able to do with the regular Windows “Task Manager”  :)

In addition to a list of processes, you can also see other info as threads, and from what module is the process.

image

PInvoke /DLLImport for Windows Mobile

Wednesday, March 26th, 2008

Using a Native DLL (c++) in a managed (c#) project is  straightforward. you just write the following in your managed code:

[DllImport("coredll.dll")]
public static extern bool CeRunAppAtEvent(string AppName, int Event);

and use the native function like it was managed.

The problem happens when you want to write your own native dll. It seems that the function in the native code must look like this

extern "C" __declspec(dllexport) void function()

confusing a bit, since when not programming for  Windows Mobile you may also write your native function without extern "C"  and add the EntryPoint attribute, and it will work… (for Windows Mobile this is not the case, you have to make sure you write extern "C") 

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 change device’s display orientation

Thursday, February 28th, 2008

In this post I describe the problem when changing the screen orientation, well 2 problems and at the end the final solution for changing the screen orientation. if you just want to read about the solution, scroll down…

In order to learn how to rotate the screen orientation I watched a MSDN How To Video, which helped but didn’t give me the right solution (hence this post :) )

By the video, in order to change the screen orientation you would write this line:

SystemSettings.ScreenOrientation = ScreenOrientation.Angle270;

This line does change the screen orientation, the problem occurs if you also want to  monitor the screen orientation change. (see How to Monitor screen orientation change) In my case I wanted my application to always be in landscape mode, meaning that in case of a screen rotation to portrait mode, I will rotate the screen back to landscape mode.

I used the SystemState change event to monitor the change and used

SystemSettings.ScreenOrientation = ScreenOrientation.Angle270;

To change the orientation back to landscape.  The screen orientation indeed changed, but after this change the SystemState change event didn’t always fire, hence in some cases my application was displayed in portrait mode. the reason for this was that the SystemState.DisplayRotation didn’t change. problem…

I thought about using the resize event, and changing the SystemSettings.ScreenOrientation.. but I got an exception.  the reason for the exception was that when being in the Resize function, I changed the SystemSettings.ScreenOrientation, making another call to the Resize function…

I found that other developers had the same problem, and saw 2 solutions, the first is to catch the exception and display to the user a message indication that the application dose not supports this mode, and the second was to use a timer (of 200 ms) inside the Resize(), that will change the orientation. I didn’t like both solutions… so I found a third one… :)

my solution:

I wanted to use the SystemState change event, since its much more efficient than the Resize event. I understood that I need to change the SystemState.DisplayRotation, but its read only…

Since the SystemState.DisplayRotation is read from the registry… I was to write the value to the registry causing it to change and hence the SystemState change event will always fire :)

problem solved!

note that SystemSettings.ScreenOrientation still needs to be changed, since its the parameter that decides the display orientation.

the code:

void OrientationWatcher_Changed(object sender, ChangeEventArgs args)
{
      int newOrientation = (int)args.NewValue;
      if (condition on newOrientation)
           ChangeToLandscape();

}
public static void ChangeToLandscape()
{
      SystemSettings.ScreenOrientation = ScreenOrientation.Angle270;
      Registry.SetValue(@"HKEY_LOCAL_MACHINE\System\GDI\ROTATION",
               "Angle", 270, RegistryValueKind.DWord);
}

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.