Instant WriteableImage in background task

I am writing an application for the Windows 8 Store with Live Tiles. Each live tile is an image that needs to be updated every x minutes. I am using a background job with a time trigger to generate my image and update the slabs.

Creating my image involves creating a new one and drawing my stuff on it, but for some reason I get an exception when I try 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 is called an interface that was a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

I got the impression that this is due to the fact that there is no user interface thread in the background job, but again, why does the WriteableBitmap require a user interface thread?

Any idea how to do this? How to create an instance of WriteableBitmap in my background task?

+4
source share
3 answers

It turns out that this is not possible in Windows 8.

This became possible in subsequent versions of Win 8.1, and then UWP through XamlBackgroundTask

+1
source

You can use Dispatcher

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

UPDATE

 Dispatcher.RunAsync (CoreDispatcherPriority.Normal, () => new WriteableBitmap(10, 10)); 
-1
source

You can still 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 likely you will get another exception (InvalidOperationException): The method was called at an unexpected time. WinRT Information: Failed to create a new view because the main window has not yet been created.

-1
source

Source: https://habr.com/ru/post/1445292/


All Articles