42Gears Speaks
24Oct/100

Garbage Collection in Windows Mobile and Low memory conditions

Garbage collector is a piece of code responsible for allocating and freeing unreferenced objects in a managed application. Garbage collection operation is invoked whenever:

  • the application calls GC.Collect() method
  • CLR cannot allocate memory for an object
  • the application loses focus and goes to the background
  • the system sends WM_HIBERNATE message to the managed application

If a Windows Mobile or Windows CE device does not have sufficient memory to allocate new objects, the system sends WM_HIBERNATE message to inactive applications; first being the longest inactive one. If WM_HIBERNATE handling by the application results in free memory sufficient for the current allocation requirement, the message is not sent to remaining apps. If the required memory is not reclaimed this way, the system starts terminating running applications by first sending them WM_CLOSE message and if required followed by calling TerminateProcess() call on them. This process also stops as soon as sufficient memory is reclaimed.

After the WM_HIBERNATE message is received by the application, a full garbage collection takes place. Its a good idea to dispose as many objects as possible during the handling of hibernate event.

Go to Home


Tagged as: No Comments
19May/100

How to get information about softwares installed on Windows Mobile devices?

Best method is to ask the configuration service provider (CSP).

Step 1: Add Reference to Microsoft.WindowsMobile.Configuration
Step 2: Add the following statements at the top of the c# code file.

using System.Xml;
using Microsoft.WindowsMobile.Configuration;

Step 3: Prepare a csp string (xml format) and pass it to ProcessConfiguration method of ConfigurationManager. Return value is the xml string which contains all currently installed softwares on the device.

private void ListInstalledSoftwares()
{
string cspString = "<wap-provisioningdoc><characteristic-query type=\"UnInstall\"></characteristic-query></wap-provisioningdoc>";
XmlDocument xmlResult = null;

// Use CSP to get list of installed applications
try
{
XmlDocument configDoc = new XmlDocument();
configDoc.LoadXml(cspString);
xmlResult = ConfigurationManager.ProcessConfiguration(configDoc, false);
Debug.WriteLine(xmlResult.InnerXml);
}
catch (Exception ex)
{
Debug.WriteLine("Failed to get list of installed applications. CSP failure. [Exception: " + ex.Message + "]");
}
}

Go to Home


10May/100

How to get process id and thread id from a Window Handle in .NET CF?

Specify the namespace for doing P/Invoke stuff i.e. calling Win32 API functions from managed code.

using System.Runtime.InteropServices;

GetWindowThreadProcessId Win32 function retrieves the identifiers of the process and thread that created the specified window.

Here is how we declare GetWindowThreadProcessId for use in managed code (c#).


[DllImport("coredll.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

Description:

  • hWnd is the window handle
  • lpdwProcessId stores the process identifier after the method returns
  • return value of the function is the id of the thread that created the window

Calling GetWindowThreadProcessId via P/Invoke:

// Set the hWnd value below with window handle of your interest
IntPtr hWnd = this.Handle;
uint processid = 0;
uint threadid = GetWindowThreadProcessId((IntPtr)hWnd, out processid);

And there you go....


16Dec/090

No Application.StartupPath in .NET CF! There is a solution though…

Too bad there is no straight-forward way in net CF for an application to determine its folder path from where it was launched. But following code works just fine.

string assembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;

string currentFolderPath = System.IO.Path.GetDirectoryName(assembly);

Enjoy...


Tagged as: No Comments