Create Scheduled code every 3 seconds

Jassim Al Rahma 1,526 Reputation points
2023-03-09T18:53:59.1566667+00:00

Hi,

I have the following in my MainPage:

    timer.Interval = TimeSpan.FromSeconds(Preferences.Get("EyeTiming", 3));
    timer.Tick += (s, e) => CheckConnection();
    timer.Start();

and the CheckConnection() is a class in MainPage that checks the Connectivity and saves it inside Sqlite.

How can I move that for iOS and Android to a background process that runs every 3 seconds even when the app is on the background or sleeping?

THanks,

Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,943 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,146 Reputation points Microsoft Vendor
    2023-03-10T02:54:37.3566667+00:00

    Hello,

    If you want to execute task in the background service continually. You need to configure each specific platform.

    For Android Please check Background Execution Limits in Android 8.0, you cannot run a background service in the background continually. If you can do it by creating a foreground service in MAUI. Then you can add scheduled code in the OnStartCommand method.

    For iOS Background tasks are under a strict time limit, and typically get about 600 seconds (10 minutes) of processing time after an application has moved to the background on iOS 6, and less than 10 minutes on iOS 7+, please refer to this document:iOS Backgrounding with Tasks

    If your application could be registered following apps.

    • Audio
    • VoIP
    • External Accessories and Bluetooth
    • Newsstand
    • Location
    • Fetch (iOS 7+)
    • Remote Notifications (iOS 7+)

    Then you can creating Background-Safe Tasks and put your scheduled code in the UIApplication.SharedApplication.BeginBackgroundTask( () => {});

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.