Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a method in the background only when app is open and running?

Once the app is open and running I would like a background process to check a database and to make an update depending on the data in the database. I would like to make this check every one minute. I only want this to happen when the app is in the foreground and in view of the user.

Can someone give me some suggestions as to how I do this? I assume I can call a method from here but I'm not sure how to do this. Also I do not know how to stop or even if I need to manually cancel / stop the process. Would it cancel itself when the app is not in the foreground and restart when the app came back into the foreground?

public partial class App : Application {     protected override void OnStart()    {       App.DB.InitData();       MainPage = new Japanese.MainPage();    } 

But do I need to make this run on a different thread and if so how could I do that.

Sorry if my question is not clear. Please ask and I can update if it doesn't make sense.

like image 826
Alan2 Avatar asked Jul 20 '17 08:07

Alan2


People also ask

How to run thread in background in Android?

To specify the thread on which to run the action, construct the Handler using a Looper for the thread. A Looper is an object that runs the message loop for an associated thread. Once you've created a Handler , you can then use the post(Runnable) method to run a block of code in the corresponding thread.

What is the current recommend way to handle long running background tasks?

Recommended solutionScheduling deferred work through WorkManager is the best way to handle tasks that don't need to run immediately but which ought to remain scheduled when the app closes or the device restarts.

What can you use for background processing in Android?

The recommended solution for deferred tasks is WorkManager. The WorkManager makes it quite efficient and easy to schedule the asynchronous and deferrable tasks, even the tasks which need to resume when the phone restarts!


2 Answers

What we did in our forms application was to make use of the Device.Timer and the Stopwatch class that available in System.Diagnostics, and Xamarin.Forms to create a very generic managed timer that we could interact with using the onStart, onSleep and onResume methods in Xamarin.Forms.

This particular solution doesn't require any special platform specific logic, and the device timer and stopwatch are non UI blocking.

using Xamarin.Forms; using System; using System.Linq; using System.Diagnostics;  namespace YourNamespace {     public partial class App : Application     {         private static Stopwatch stopWatch = new Stopwatch();         private const int defaultTimespan = 1;          protected override void OnStart()         {             // On start runs when your application launches from a closed state,               if (!stopWatch.IsRunning)             {                 stopWatch.Start();             }              Device.StartTimer(new TimeSpan(0, 0, 1), () =>             {                 // Logic for logging out if the device is inactive for a period of time.                  if (stopWatch.IsRunning && stopWatch.Elapsed.Minutes >= defaultTimespan)                 {                     //prepare to perform your data pull here as we have hit the 1 minute mark                             // Perform your long running operations here.                          Device.InvokeOnMainThread(()=>{                             // If you need to do anything with your UI, you need to wrap it in this.                         });                      stopwatch.Restart();                 }                  // Always return true as to keep our device timer running.                 return true;             });         }          protected override void OnSleep()         {             // Ensure our stopwatch is reset so the elapsed time is 0.             stopWatch.Reset();         }          protected override void OnResume()         {             // App enters the foreground so start our stopwatch again.             stopWatch.Start();         }     } } 


Edit:

To give some context as to how the above solution works step by step:

The application starts from a closed state and the 'OnStart()' method creates our Device.Timer that ticks every second. It also starts our stopwatch that counts upto a minute.

When the app goes into the background it hits the 'OnSleep' method at this point if we were to pass a 'false' value into our Device.StartTimer() action it would not start up again. So instead we simply reset our stopwatch ready for when the app is opened again.

When the app comes back into the foreground it hits the 'OnResume' method, which simply starts the existing stopwatch.

2018 Edit:

This answer still has some merits even in 2018, but mainly for very specific situations. There are better platform specific ways to replicate this functionality even in Xamarin.Forms. The above still remains a platform agnostic way to perform a task after a period of time, taking into account user activity/inactivity.

like image 170
JoeTomks Avatar answered Sep 20 '22 06:09

JoeTomks


you can use this,

 System.Threading.Tasks.Task.Run(() =>  {       //Add your code here.  }).ConfigureAwait(false); 
like image 21
Manish Sharma Avatar answered Sep 23 '22 06:09

Manish Sharma