Instantiate WriteableImage in Background Task

4

I am writing a Windows 8 Store App with Live Tiles. Every live tile is an image that needs to be refreshed every x minutes. I am using a Background Task with a time trigger to generate my image and refresh the tile.

Generating my image implies creating a new one and paint my stuff on it but for some reason I am getting an exception when trying to create a new instance of WriteableBitmap:

var newImage = new Windows.UI.Xaml.Media.Imaging.WriteableBitmap(10, 10);

or

var newImage = BitmapFactory.New(10, 10);

throws this exception:

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

I have an impression this is because there is no UI thread in the Background Task but then again, why would the WriteableBitmap require an UI thread?

Any idea how to workaround this? How would I instantiate the WriteableBitmap in my background task?

windows-8
windows-runtime
asked on Stack Overflow Nov 11, 2012 by AlexDrenea

3 Answers

1

Turns out this was not possible in Windows 8.

It was made possible in subsequent versions Win 8.1 and then UWP via the XamlBackgroundTask

answered on Stack Overflow Jun 20, 2016 by AlexDrenea
-1

You can use Dispatcher

Application.Current.Dispatcher.Invoke(
    DispatcherPriority.Normal,
    new Action(() => createBitmap)
);

UPDATE

Dispatcher.RunAsync
    (CoreDispatcherPriority.Normal, 
    () => new WriteableBitmap(10, 10));
answered on Stack Overflow Nov 11, 2012 by Mayank • edited Nov 11, 2012 by Mayank
-1

You still can use the following code:

 CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
            () =>{
                // Your UI update code goes here!
                WriteableBitmap writeableBmp = BitmapFactory.New(imageWidth, imageHeight);
            });

But most probably you will get another exception (InvalidOperationException) saying: A method was called at an unexpected time. WinRT information: Could not create a new view because the main window has not yet been created.

answered on Stack Overflow Oct 16, 2013 by MehranTM • edited Oct 16, 2013 by MehranTM

User contributions licensed under CC BY-SA 3.0