locked
Question on executing operations in Background

    Question

  • Hi All,

     

    I am building up an universal app targeting 8.1 runtime and also new to whole .NET world.

     

    I have some operations  in my application which I want to perform in parallel, asynchronously and also in background. These operations include file upload, download (both I/O and compute bound). So I wanted to execute them In threads.  I would like to process them in 3 threads. Each thread will work on the operation queue and keep waiting perpetually (in while(true) loop) unless an operation is available for execution. These threads would also pass task status or progress updates to UI.

     

    Would also like to have these threads running on Application lock or if the app suspends . If my application terminates or if the network connections is lost these threads will stop. Also would like to cancel the operations running in the threads according to me requirements (like cancelling a file download).

    My initial hunch was to use threads. But since threads are not available for windows store apps and my exploration about threading in Windows store apps revealed to use task to perform this.

     

    I read about the Tasks Asynchrony pattern (TAP) whitepaper published by MS ,channel 9 videos from Bruce Kyle and lot of other blogs. I am convinced to use Tasks .As it runs on a thread pool which can take advantage  of multi core processor and is better performing

     

    Questions on Tasks will be

    1. Will it be useful as background thread. Can tasks be run as a background thread perpetually.
    2. I don't really need my tasks to return results. The result from the operations can be relayed to UI through events.
    3. Do I have to schedule my tasks from a background task API when the app suspend ?

     

    Does the Task API fit my scenario ?

    Are Background tasks in Windows same as services in Android.  ?

     

    Thanks a lot and Regards,

    Saurav

     

    Thursday, January 29, 2015 3:56 PM

Answers

  • You app's threads will all suspend when the app suspends. If you need to continue running in the background you will need to register a background task.

    BackgroundUploader and Downloader are the best ways to accomplish uploading and downloading in the background.

    For other functionality in the background it depends on what specifically you need to do. Background processing is deliberately limited, but you can create Background Tasks for specific purposes. As hereafter suggested, please see Guidelines for background tasks

    Friday, February 6, 2015 12:47 AM
    Owner
  • You'll need to use the background transfer API to upload and download in the background. General purpose background tasks don't have enough resources to transfer a significant file. You can monitor the background tasks from the foreground app when it runs.

    Friday, February 6, 2015 9:43 PM
    Owner

All replies

  • 1. When your app becomes suspended in the background. all your threads will be paused.

    2. Windows background tasks are very different from android services.

     Background tasks are designed to accomplish certain well defined tasks. They are more similar tobackground tasks on iOS.


    https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977051.aspx

    • Edited by hereafter Thursday, January 29, 2015 4:07 PM
    Thursday, January 29, 2015 4:05 PM
  • Thanks for the reply Hereafter.

    then how can i keep some background operations running ?

    I learnt that there are BackgroundUploader and BackgroundDownloader classes which help to accomplish this . Are those classes the only way to do this ? But i need some functionality to be done more than upload and download of files.

    cheers,

    Saurav

    Thursday, January 29, 2015 5:40 PM
  • You app's threads will all suspend when the app suspends. If you need to continue running in the background you will need to register a background task.

    BackgroundUploader and Downloader are the best ways to accomplish uploading and downloading in the background.

    For other functionality in the background it depends on what specifically you need to do. Background processing is deliberately limited, but you can create Background Tasks for specific purposes. As hereafter suggested, please see Guidelines for background tasks

    Friday, February 6, 2015 12:47 AM
    Owner
  • Thanks Rob for the reply.

    I already have a library API for my domain which does this job of uploading and downloading. Consumers of this library only work with the domain objects and its APIs. Currently i call this APIs from my perpertual Tasks which gives me asynchronous behaviour in foreground

    My question here would be:

    Can my perpetual tasks/threads be activated/started again from the background tasks when the app goes to background ? Even if it runs for sometime i can send a message to UI to user depicting the termination of my operations.

    Appreciate your help a lot.

    Best Regards,

    Saurav

    Friday, February 6, 2015 4:11 AM
  • You'll need to use the background transfer API to upload and download in the background. General purpose background tasks don't have enough resources to transfer a significant file. You can monitor the background tasks from the foreground app when it runs.

    Friday, February 6, 2015 9:43 PM
    Owner