Archive for May, 2008

Safe Cross Thread UI Access

Wednesday, May 21st, 2008

When you have multiple threads running in your application and have a user interface you’ll eventually run into the fact that UI elements are only allowed to be called from the same thread they are created in. The fast workaround for this is to set the CheckForIllegalCrossThreadCalls property of your controls to false but since we’re all good developers we want to solve this in a clean way.

There is an interface to track this behaviour called ISynchronizedInvoke which defines all the methods you need, this interface is implemented by the UI controls. You need to see if the property InvokeRequired is true and call Invoke (or BeginInvoke) to make your call thread safe. I used to declare a delegate for each different method signature I encountered since the Invoke methods take a delegate as argument. But there is a much more elegant way by using the MethodInvoker delegate and an anonymous delegate as you can see below. This prevents you from declaring a lot of delegates just for making your calls thread safe. This is illustrated below by a method in a presenter which updates my view.

public void IncomingMessage(ChatMessage msg)
{
    if (m_ChatView != null && m_ChatView.InvokeRequired)
    {
        m_ChatView.BeginInvoke(new MethodInvoker(delegate
        {
            IncomingMessage(msg);
        }), new object[] { msg }); return;
    }
    m_ChatView.DisplayMessage(BuildIncomingDisplayMessage(msg));
}

MIX essentials

Tuesday, May 13th, 2008

A few weeks ago, the 24th of april to be exact, the Softelligent development team went to MIX essentials in Louvain-la-Neuve. The MIX essentials events are a slimmed down version of the American MIX 08 events and give an overview of what was handled on the American mainland. Luc Vandevelde was the main host and introduced the foreign and not so foreign speakers.

mix1

The event consisted of 2 tracks, one focused on development and the other on “men in suits” topics. We followed the development track which gave an introduction to Silverlight and MSN Live services and how to integrate them in your own applications.

The final keynote speaker was “Steve B” as he calls him self. The current CEO of Microsoft shared his view on IT in general and spent almost half of his time answering questions from the audience. Literally everyone was hanging on his lips, mister Ballmer is a very charismatic person. Although he didn’t perform his signature monkey dance, despite someone mentioning it during a question.

mix2

The event itself was ok, Silverlight looks pretty awesome, especially the Deep Zoom functionality. But nothing groundbraking was shown, seeing and hearing Steve B on the other hand was worth the trip anyway. I’ll probably never see someone who is richer for the rest of my life.