Forum Xamarin.Mac

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Why is my MacOS App Napping despite me instructing it not to

System.AshSystem.Ash AUMember ✭✭

TL;DR: Repro is here - https://github.com/FunkyLambda/AppNapRepro

Although this is happening within a Xamarin.Mac project, I think the issue is more to do with MacOS, as the signs are that of App Nap.

In my AppDelegate.cs file, I have this:

public override void DidFinishLaunching(NSNotification notification)
{
    _activity = NSProcessInfo.ProcessInfo.BeginActivity(
                    NSActivityOptions.Background |
                    NSActivityOptions.LatencyCritical,
                    "You charge $3,500AUD for this laptop and yet " +
                    "can't provide enough resources for a smooth " +
                    "operation of this app in the background? " +
                    "Eat a d#@k Apple."
                );
    // ...

I tested the above with the following that runs every ~1s:

var now = DateTime.Now;
var now_str = now.ToString("HH:mm:ss.fff");
Debug.WriteLine(now_str);
var idle_time = now - _prevTime;
if (idle_time.TotalMilliseconds > 10000)
{
    Debug.WriteLine("FFSakes Apple!");
}
_prevTime = now;
await Task.Delay(1000); // yes yes, I know timers aren't precise, hence why I said '~'1s.

After hours of banging my head, I decided to run the app, put it in the background and go have a nap. Turns out, so did my app. In the 1.5 hours I left it on, it did this:

19:23:29.040
19:23:30.041
19:56:07.176
FFSakes Apple!
19:56:08.196
19:56:09.196
...

A lag of over half an hour!

Question 1: Why?
Question 2: How do I get around this?

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    May I ask what you want to test here?
    The value of now will always be 1 greater than _prevTime and the program on my side won't ever run into if (idle_time.TotalMilliseconds > 10000) statement.

  • System.AshSystem.Ash AUMember ✭✭

    @LandLu It's not what I want to test - I've already performed the tests. It's what I want to overcome. For me the if statement is hit several times. Did you perform the reproduction steps exactly as described in the repro? How long did you leave it running for with the computer locked?

  • LandLuLandLu Member, Xamarin Team Xamurai

    I didn't test this for 1.5 hours. If you leave the machine with so long time it will come to sleep mode and application runs into background. Therefore, timer will freeze in the background. This makes sense.
    However, we will not spend several hours testing this extreme situation.

  • System.AshSystem.Ash AUMember ✭✭

    @LandLu As per my repro instructions, you can trigger sleep sooner if you lock the screen. For me, the if statement triggered 2 mins into locking my machine.
    Regardless, if you agree the timer will freeze despite the fact that I've instructed it not to in AppDelegate.cs (which is the whole point of this question), how do I get around it?
    My app needs to run in the background indefinitely.

Sign In or Register to comment.