Friday, October 26, 2007

SystemState StateChanged Subscription Must Occur on UI Thread?

I posted the following to the .NET CF newsgroup this morning:

The Microsoft.WindowsMobile.Status.SystemState class offers a great way to track a number of interesting and useful device system states (e.g., incoming phone calls, battery strength, etc.). However, recently I observed that the SystemState.Changed += new ChangeEventHandler(OnSystemStateChanged) subscription must be made from the UI thread. Has anyone else run into this? I attempted to write a console app that uses many of these system state notifications; however, none of the events were actually occurring. Once I switched to a form-based application, everything worked great.

Is this documented somewhere? If so, I couldn't find it. I am programming in .NET CF 2 for Windows Mobile 5.0 devices.


Chris Tacke responded with,

Most of those events come from windows messages being posted at a low level (FileSystemWatcher works int he same way too). If you never call Application.Run, no message pump is ever created, so no windows messages are ever dispatched.

I don't think I've ever seen it documented, but if you walk through the aygshell source code in Platform Builder (which a lot of these come from eventually), you'll see that's how they work. I agree that it's a poor choice, but it's how it was implemented. The workaround is a very long, tedious process (again, we've done it for file system notifications) of reimplementing the entire listening system, creating a hidden window for your app or implementing a Run() that doesn't take a Form (like we did in the SDF).

Thursday, October 25, 2007

Image Download in .NET CF

Even though it seems more popular to use the WebClient class to download a single image or two, that class is not available on .NET CF. Nonetheless, it's easy enough with WebRequest and WebResponse.

//grab icon
Uri uri = new Uri("valid url to image");
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream imageStream = httpResponse.GetResponseStream();
Bitmap buddyIcon = new Bitmap(imageStream);
httpResponse.Close();
imageStream.Close();

//save icon as jpeg
string iconFilePath = Path.Combine(FullContactsPath, contact.UserId + ".jpg");
buddyIcon.Save(iconFilePath, ImageFormat.Jpeg);

Tuesday, October 02, 2007

Security Manager for WM5

Though the Device Security Manager PowerToy for Windows Mobile 5.0 has been available since June 6th, 2006, it's such an important application that it deserves mention even a year after its release. For one thing, it allows developers to easily bypass the incessant prompting that occurs every time a binary is changed and loaded on the device. And for two, it allows you as a developer to test different security policies on one device.

The website says:

Device Security Manager helps developers test various security policies for Windows Mobile devices. It is designed as a desktop application that ships w ith a preset list of “security configurations”. A security configuration can be thought of as a template, which contains a collection of individual policies and settings. For example, a security configuration could define policies such as whether unsigned applications are allowed to execute, whether RAPI is disabled etc. Using this tool, the developer can provision a Windows Mobile device with different configurations, and then test the application’s behavior under these configurations. This tool can be used either on an emulator or an unlocked Windows Mobile device.