在Xamarin中每20分钟提出一种方法

时间:2019-11-23 12:06:43

标签: c# multithreading xamarin xamarin.forms threadpool

我正在尝试提出一种每20分钟获取一次电池电量的方法。因此我的应用程序应该以正常方式运行,而我的方法每隔20分钟就会在后台调用一次。

因此,为了获取电池电量,我使用了Nuget的Xam.plugin.Battery,所以这不是问题。至于其他部分,我认为我应该为此使用另一个线程,以便每20分钟在后台调用一次我获取电池的方法时,我的应用程序可以正常运行。问题是:我不知道该怎么做。

谢谢

1 个答案:

答案 0 :(得分:0)

您想要的是启动计时器。

    Device.StartTimer(TimeSpan.FromMinutes(20), () => //Will start after 20 min
    {
        Task.Run(async () =>
  {
    var time = await DoYourBatteryWork();
    // do something with time...
  });

        return true; // To repeat timer,always return true.If you want to stop the timer,return false
    });    
}