I have a Windows Phone 8.0 app that I'm porting to 8.1. In 8.0 I depended a lot on BackgroundWorker to execute tasks that I didn't want consuming the UI thread.
I would create the BackgroundWorker, define the DoWork() delegate and then immediately execute RunWorkerAsync()
Now in 8.1 I can't use BackgroundWorker anymore. Instead, I need to create Tasks implementing IBackgroundTask and use IBackgroundTrigger objects run them.
It seems like I need to jump through a lot of hoops just to run code on a different thread. If I want to run a background task immediately to I create a time triggered background task with a new oneShot TimeTrigger() with 0 freshness minutes? That seems like a bit of a hack..
Is there an alternative to BackgroundTask? Should I be approaching my requirements differently?
To run anything on a different thread, all you need to do is call:
Task.Run(delegate() {
// The work to be executed on the background thread
});
You can also await this in a non-blocking way, in case you want to do something after the work in the different thread has finished.
IBackgroundTask is something completely different. It's used when you want to have some code executed on some event when the app is not running. For example, if you want to update the Live Tile every 30 minutes, you would do this using a background task, implementing that interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With