C#

XML and JSON (De)Serialization in WinRT and Windows Phone

In some of my recent Metro/WinRT projects I had to save data to the disk. I decided to go for Xml-serialzation, because it’s human-readable, and flexible. Also it was perfect for the data I wanted to save (eg. metadata about a movie). (If you want to do it on your own: System.IO and System.Xml.Serialization are the namespaces you need for XML (de)serialization.) As I needed this in many projects, I composed a small library, containing a TCD.Serialization.XmlDeSerializer as well as a TCD.Serialization.JsonDeSerializer. Both have four static methods to serialize to/from Stream and string. You can download a package with the sources and a ready-to-use dll. TCD.Serialzation is available on NuGet it works with Windows Phone as well. NOTE: the methods aren’t fail-safe, so you might need to try{}catch{} them to handle exceptions. (For example if you want to serialize to a FileStream without write permission.) Have fun and let me hear…

C#

Performing long-running, timed operations on the UI thread

Recently I had to play timed sounds (morse code) in a WP7 applications. Sounds are required to be played on the UI thread, but as I had to time them, my UI thread got frozen. The reason was, that I used Thread.Sleep() to time the sound, which of course blocked the UI thread. Today I figured out a way to do this long-running operation (many seconds..) without blocking the UI thread. You may know the Visual Studio Async CTP – it’s required to do the trick. Let’s suppose we already have a method InvokeEvent() that plays the sound, changes a color or something like that. Currently we execute this method in a pattern like this, causing the UI thread to be blocked: void StartOperation()//invoke the event in a pattern { for (int i=0; i<20; i++)//20 times { InvokeEvent();//play the sound, blink with the display or something like that Thread.Sleep(500);//this will…