这是我想出的代码。它似乎有效,但我担心这可能不是做我想做的事情的好方法。我需要的是在 OnAppearing 发生时每分钟运行一个方法,并让它随着 OnDisappearing() 停止;

protected async override void OnAppearing()
{
   base.OnAppearing();
   BindingContext = vm;

   cts = new CancellationTokenSource();
   if (Settings.mode == MO.Practice)
   {
      if (!App.stopWatch.IsRunning) { App.stopWatch.Start(); }
            Device.StartTimer(new TimeSpan(0, 0, 5), () =>
      {
          if (App.stopWatch.IsRunning && App.stopWatch.Elapsed.Seconds >= 60)
          {
             // Here's the method I want to run. After it's finished
             // I call BeginInvoke .. to update info on the screen
             if (App.DB.ReducePoints() == true)
                Device.BeginInvokeOnMainThread(() =>
                {
                   vm.PifInfo = GetPifInfo();
                });
                App.stopWatch.Restart();
             }
             return true;
            });
        }
        await GetCards(cts.Token);
    }
}

protected override void OnDisappearing()
{
    Unsubscribe();
    cts.Cancel();
    if (App.stopWatch.IsRunning) { App.stopWatch.Stop(); }
    base.OnDisappearing();
}
不是问题的一部分,但我也欢迎对代码提出任何意见。

最佳答案

您可以通过从 Device.StartTimer 返回正确的值、重复 true 、不重复 false 和不使用 StopWatch 来更简单地做到这一点。 ( source 声明 While the callback returns true, the timer will keep recurring. 并且正如您从源代码中看到的,该方法不需要 Func<Task<bool>> 它只需要一个 Func<bool> 回调,因此不需要使用 Task 。)
类中的

volatile bool run;
OnAppearing 中的
run = true;
Device.StartTimer(new TimeSpan(0, 1, 0), () => {
if (run) { /*do what you want;*/ return true; }
else { return false; }
});
在 OnDisappearing
run = false;

编辑 - 根据 OP 的要求
这是代码。我将留下我的原始答案以帮助需要此功能的任何其他人。
volatile bool run;

protected async override void OnAppearing()
{
   base.OnAppearing();
   BindingContext = vm;
   cts = new CancellationTokenSource();
   if (Settings.mode == MO.Practice)
   {
        run = true;
        Device.StartTimer(new TimeSpan(0, 1, 0), () =>
        {
            if (run)
            {
                 if (App.DB.ReducePoints() == true)
                    Device.BeginInvokeOnMainThread(() =>
                    {
                       vm.PifInfo = GetPifInfo();
                    });
                 return true;
            }
            else { return false; }
        });
        await GetCards(cts.Token);
    }
}

protected override void OnDisappearing()
{
    run = false;
    Unsubscribe();
    cts.Cancel();
    base.OnDisappearing();
}

关于c# - 我想每分钟运行一个从我的 OnAppearing() 开始的方法。我需要将此作为任务运行吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54388183/

10-11 22:45
查看更多