WildernessLabs / Meadow_Issues

Public repo for bugs and issues with Meadow
15 stars 0 forks source link

[Internal] Meadow.OS - TimeSpan issue #740

Closed jorgedevs closed 4 months ago

jorgedevs commented 4 months ago

Running with binaries v1.12.6.4

I have a my WiFiWeather Sample that has an infinite While loop, where in each cycle, i wait for one minute to either do an API call (to get Weather data) or just update the date time on my display.

image

I added a Console Output on each cycle, and I found out that the while loop iterates every 5 seconds approx and not every minute like I have coded in my app.

The loop looks like this:

while (true)
{
    displayController.UpdateWiFiStatus(hardware.NetworkAdapter.IsConnected);

    if (hardware.NetworkAdapter.IsConnected)
    {
        var today = DateTime.Now.AddHours(hardware.TimezoneOffset);

        Resolver.Log.Trace($"Today: {today.ToString("hh:mm tt | dd/MM/yyyy")}");

        if (today.Minute == 0 ||
            today.Minute == 10 ||
            today.Minute == 20 ||
            today.Minute == 30 ||
            today.Minute == 40 ||
            today.Minute == 50 ||
            firstWeatherForecast)
        {
            Resolver.Log.Trace("Getting forecast values...");

            await UpdateOutdoorValues();

            Resolver.Log.Trace("Forecast acquired!");
        }

        displayController.UpdateStatus(today.ToString("hh:mm tt | dd/MM/yyyy"));

        await Task.Delay(TimeSpan.FromMinutes(1));
    }
    else
    {
        Resolver.Log.Trace("Not connected, checking in 10s");

        await Task.Delay(TimeSpan.FromSeconds(10));
    }
}

Basically, I check im connected to my network, to iterate once every minute to update my Date and Time on my HMI screen, and every 10 minutes, I do a call to the web service to get my Weather data.

Things I've tried:

adrianstevens commented 4 months ago

@jorgedevs can you paste the loop code here?

adrianstevens commented 4 months ago

I assume it's this? await Task.Delay(TimeSpan.FromMinutes(1));

I'm curious to know if Thread.Sleep works

adrianstevens commented 4 months ago

And try it without a TimeSpan ... just pass an int for milliseconds

jorgedevs commented 4 months ago

@adrianstevens excellent questions, thank you. I answered those questions updating the issue to give more context.

ctacke commented 4 months ago

Since the test app above is incomplete, I created a new one (also attached). This code, running on 1.12.2, works fine.

public class MeadowApp : App<F7FeatherV2>
{
    public override Task Run()
    {
        Resolver.Log.Info($"Device running {Device.Information.OSVersion}");

        Task.Run(async () =>
        {
            while (true)
            {
                Resolver.Log.Info("TICK [Task.Delay FromMinutes]");
                await Task.Delay(TimeSpan.FromMinutes(1));
            }

        });

        Task.Run(async () =>
        {
            while (true)
            {
                Resolver.Log.Info("TICK [Task.Delay FromSeconds]");
                await Task.Delay(TimeSpan.FromSeconds(60));
            }

        });

        new Thread(() =>
        {
            while (true)
            {
                Resolver.Log.Info("TICK [Thread.Sleep FromSeconds]");
                Thread.Sleep(TimeSpan.FromSeconds(60));
            }
        }).Start();

        new Thread(() =>
        {
            while (true)
            {
                Resolver.Log.Info("TICK [Thread.Sleep FromMinutes]");
                Thread.Sleep(TimeSpan.FromMinutes(1));
            }

        }).Start();

        return base.Run();
    }
}