Messagebox.Show Breaks Dispatcher.BeginInvoke after DataServicesQuery on Windows Phone 7

I’m guessing this is an issue with the current CTP of the Windows Phone 7 bits, or perhaps just an emulator issue, but it appears that if you call MessageBox.Show it throws a spanner in the works when you try to do a UI update from a background thread.

Take for example if you were to follow Alex’s post on Accessing WCF Services then you would be calling BeginExecute on a DataServiceQuery. As you would imagine this goes off and does an asynchronous call. When the call is complete the callback method (in our case an anonymous method) is invoked. In the following sample code all the call back does is to call Dispatcher.BeginInvoke to execute a method that will refresh the UI.

In the following code, SimpleUpdate is never executed.

        ServiceEntities context;

        void SimpleUpdate()
            this.button.Content = "Updated Text";

        private void button_Click(object sender, RoutedEventArgs e)
            MessageBox.Show("Hi Everyone!");

            context = new ServiceEntities(new Uri(http://myserver/Services/myservice.svc/));

            var query = context.Customers;
                       (asyncResult)=> {

                                                 },  query);


If we remove the MessageBox.Show line, the code executes without an issue and SimpleUpdate is called and the button text is updated.

In my case I came across this because I was using the MessageBox for debugging. In a real world Windows Phone application you really need to reconsider the use of MessageBox at all – it’s generally a symptom that something hasn’t been designed well or that something has gone wrong (eg exception thrown).

Leave a comment