0

在我的应用程序中,即使应用程序在后台运行,我也想运行计时器。我想有两种方法可以做到这一点

  1. 使用线程
  2. 捕获应用程序进入后台并返回的时间

我不知道哪种方式更好?如果我使用线程,那么它可以工作多长时间?它在后台工作几个小时吗?

谢谢

4

1 回答 1

0

如果您的应用程序进入后台状态,您没有太多时间停止当前线程。

你可以像这样运行后台任务:

...
public partial class AppDelegate : UIApplicationDelegate
{
    ...
    public override void DidEnterBackground (UIApplication application)
    {
        int taskID = UIApplication.SharedApplication.BeginBackgroundTask ( () => {});
        var task = new Thread (new ThreadStart (() => { 
            // Save application's current state.
        }));
        task.Start ();
    }

因此,您将拥有最多10 minutes.

然而iOS不允许无限运行应用程序。请参阅BackgroundExecution演示以供参考。

于 2013-05-08T17:03:47.940 回答