12

In WP7 app I am calling some code to update a live tile from the onInvoke method on the ScheduledAgent class.

Getting an invalid cross-thread error.

The line it is failing on is

var fontForeground = new SolidColorBrush(Colors.White);

I understand that there are limitations ans API's that I cannot call from within this background task, but I am only trying to setup some stuff to generate my own image to display on the Live tile.

Is there something obvious I am doing wrong here.

  • thanks

Update...

I have discovered this question

How can I render text on a WriteableBitmap on a background thread, in Windows Phone 7?

It is the same issue that I have and whilst there is some good dialogue on here it doesn't appear that there is an easy way to get around this?

1 Answer 1

36

Use the Dispatcher to execute the code on the UI thread instead on a background thread:

Deployment.Current.Dispatcher.BeginInvoke(()=>
    { 
         fontForeground  = new SolidColorBrush(Colors.White);
         ...        
   });
3
  • Thanks for the quick answer. That line does not compile "Cannot convert lambda expression to type "System.Windows.Threading.DispatcherPriority" because it is not a delegate type. You mention executing the code ina UI thread - will this same code work within the foreground application. I am trying use the same piece of code for both foreground and background.
    – Peter
    Sep 15, 2011 at 9:21
  • edited my answer. The above code will execute on the UI thread. Although I'm not sure if this solves your problem, as I'm not familiar with ScheduleAgent, but it is worth a try :) Sep 15, 2011 at 9:42
  • sorry - still couldn't get it to syntax.
    – Peter
    Sep 15, 2011 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.