Search code examples
c#androidmultithreadingandroid-servicebackgroundworker

BackgroundWorker vs. Android Service in Xamarin


I'm investigating about mobile apps using Mono on Visual Studio.Net.

Currently we have an application we want to translate to Android from Windows CE. The original program used small BackgroundWorkers to keep the UI responsive and to keep it updated with the ProgressChanged event. However I have been reading that in Android there are Services that can replace that functionality.

Reading pros and cons about services I know that they are usually used because they have a better priority than threads and, mainly, if the functionality will be used in more than one app.

More info I have found comparing threads and Services say that Services are better used for multiple tasks (like downloading multiple files) and threads for individual tasks (like uploading a single file). I consider this info because BackgroundWorker uses threads.

Is there something I am missing? Basically a service should be for longer tasks because the O.S. gives it better priority (there are less risk it will be killed) and Threads/BackgroundWorkers are better for short tasks. Are there any more pros/cons to use one or the other?

Thank you in advance!

[Edit] If you need a very specific question... how about telling me when and why would you use a Service instead of a BackgroundWorker? That would be useful.

Some of the functionality I have to recreate on Android: - GPS positioning and compass information - this has to be working most of the time to get the location of the device when certain events are working and trace in a map its movements. - A very long process that might even be active for an hour.

The last one is the one I am concerned about. It must be very reliable and responsible, keeping the user informed of what it is doing but also being able to keep working even if the user moves to other activity or functionality (doing a call, hitting the home button, etc.)

Other than that I believe the other functionality that used BackgroundWorker on WinCE will not have problems with Android.

[Edit 2: 20140225] However I would like to know if the AsyncTask can help me in the next scenario: - The app reads and writes information from/to another device. The commands are short in nature and the answer is fast so for individual commands there is no problem. However there is a process that can take even an hour or so and during that time it will be asking the status from the device. How would you do it?


Solution

  • I think you're misunderstanding what a Service in Android is. See the documentation on Services:

    A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application.

    Also note:

    A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise).

    Using a worker thread and using a Service are not mutually exclusive.

    If you are looking to move work off the main thread, then clearly you need to use another thread. Through a BackgroundWorker or perhaps the TPL will do just fine in many cases but if you want to interact with UI (e.g. on completion of the task or to update progress in the UI), the Android way is to use an AsyncTask (mono docs).

    If this work needs to continue outside of the user interaction with your application, then you may want to host this work (including the BackgroundWorker/Thread/AsyncTask/etc.) in a Service. If the work you want to do is only ever relevant while the user is interacting with your application directly, then a Service is not necessary.