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?

Community
  • 1
  • 1
Peter
  • 679
  • 1
  • 10
  • 25

1 Answers1

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);
         ...        
   });
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
  • 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 '11 at 09: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 :) – thumbmunkeys Sep 15 '11 at 09:42
  • sorry - still couldn't get it to syntax. – Peter Sep 15 '11 at 09:53