Multithreading in Windows Phone 7 emulator: A bug

Multithreading is supported in Windows Phone 7 Silverlight applications, however the emulator has a bug (which I discovered and was confirmed to me by the dev lead of the emulator team): If you attempt to start a background thread in the MainPage constructor, the thread never starts. The reason is a problem with the emulator UI thread which doesn’t leave any time to the background thread to start. Thankfully there is a workaround (see code below). Also, the bug should be corrected in a future release, so it’s not a big deal, even though it is really confusing when you try to understand why the *%&^$£% thread is not &$%&%$£ starting (that was me in the plane the other day ;)

This code does not work:

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();

        SupportedOrientations = SupportedPageOrientation.Portrait
            | SupportedPageOrientation.Landscape;

        var counter = 0;

        ThreadPool.QueueUserWorkItem(o =>
        {
            while (true)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    textBlockListTitle.Text = (counter++).ToString();
                });
            }
        });
    }
}

This code does work:

public MainPage()
{
    InitializeComponent();

    SupportedOrientations = SupportedPageOrientation.Portrait
        | SupportedPageOrientation.Landscape;

    var counter = 0;

    ThreadPool.QueueUserWorkItem(o =>
    {
        while (true)
        {
            Dispatcher.BeginInvoke(() =>
            {
                textBlockListTitle.Text = (counter++).ToString();
            });

            // NOTICE THIS LINE!!!
            Thread.Sleep(0);
        }
    });
}

Note that even if the thread is started in a later event (for example Click of a Button), the behavior without the Thread.Sleep(0) is not good in the emulator. As of now, i would recommend always sleeping when starting a new thread.

Happy coding:

Laurent

 

Print | posted on Friday, March 19, 2010 2:31 PM

Feedback

# re: Multithreading in Windows Phone 7 emulator: A bug

left by Zack at 4/5/2010 6:46 AM Gravatar
Wow, thank you for this. I'm not sure why (maybe it's me) but I can't seem to find any good info on just how to code for Windows Phone 7 yet. Maybe b/c it's so new, but this is a good solid example that I implemented and had it actually work... FINALLY.

# re: Multithreading in Windows Phone 7 emulator: A bug

left by Ryan Lowdermilk at 9/10/2010 8:17 AM Gravatar
Huge help! Thank you!

# re: Multithreading in Windows Phone 7 emulator: A bug

left by Florian Bruggisser at 3/18/2011 1:24 PM Gravatar
Thanks for this helpful article, but I do have another question:
We need to implement synchronous calls for the WP7, because our business logic bases on a synchronous webrequests.

I thought, I could synchronize them, with multi threading. But the GUI thread is always the one, which receives events and so, the app freezes, if my second thread created the webrequest and called "DownloadStringAsync". The main (GUI) thread never get the event because, he is waiting for the signal of the second thread. (thats trick^^ but I hope you understand what i mean)

So, and now, my questions is: Is it possible to attach an event to another thread? Or do you know any other solution for this problem? Rewriting the whole business logic isn't possible.

Best regards
Florian Bruggisser
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: