ios - 完成 iOS 后台任务,将其移动到前台

标签 ios multithreading xamarin.ios background-process

我使用来自 http://software.tavlikos.com/2010/12/08/multitasking-in-ios-the-monotouch-way-part-i/ 的代码示例在 iOS 应用程序 DidEnterBackground 事件上运行后台任务的文章。

任务繁重且时间长 - 将近 1 分钟。如果用户强制应用程序回到前台,他应该等到任务完成。有点弱。

我尝试使用 WillEnterForeground 事件:

    public override void WillEnterForeground (UIApplication application)
    {
        if (taskId != 0) {
            Console.WriteLine("Finish previous task"); 
            application.EndBackgroundTask(taskId);
            taskId = 0;
        }
    }

但它会在任务结束后调用。

如果用户在完成之前切换到应用程序,如何中断后台任务执行?

DidEnter后台代码:

    public override void DidEnterBackground (UIApplication application)
    {
        Console.WriteLine ("DidEnterBackground");

        if (taskId != 0) {
            Console.WriteLine("Finish previous task"); 
            application.EndBackgroundTask(taskId);
            taskId = 0;
        }

        taskId = application.BeginBackgroundTask(delegate {
            if (taskId != 0) {
                application.EndBackgroundTask(taskId);
                taskId = 0;
            }
        });

        Console.WriteLine("Begin task"); 
        application.InvokeOnMainThread(delegate() {
            for(int i = 0; i < 700; i++) {
                if ((i + 1) % 100 == 0)
                    Console.WriteLine("Time to remain: {0} seconds", application.BackgroundTimeRemaining); 
                //viewController.Touch();
            }
        });

        application.EndBackgroundTask(taskId);
        taskId = 0;
    }

最佳答案

主要问题是您的 DidEnterBackground 方法只会在您的 for 循环完成后返回(或者,无论您执行了哪个任务),因为您是在同一个线程上调用它。使用或不使用 InvokeOnMainThread 并不重要,您的任务将在同一个线程上执行,实际上,您不需要调用 InvokeOnMainThread 因为您已经在主线程上。在正常情况下,这会导致您的应用程序被 iOS 终止。但是,由于您首先调用 BeginBackgroundTask,因此不会发生这种情况。

因此,由于您的 DidEnterBackground 方法的返回被延迟并且您同时将您的应用程序带回前台,因此执行您的 WillEnterForeground 方法是正常的 你的任务结束之后。这是因为 WillEnterForeground 方法无法执行,因为 DidEnterBackground 方法还没有返回,所以它在之后排队。

在您提到的上述文章中的示例中,我通过调用 new System.Action(SomeDelegate(){}).BeginInvoke(null, null); 异步调用我的任务。您可以使用任何您喜欢的解决方案,我现在主要使用 ThreadPool

所以,我会像这样修改上面的 DidEnterBackground 方法:

    public override void DidEnterBackground (UIApplication application)
    {
        Console.WriteLine ("DidEnterBackground");

        if (taskId != 0) {
            Console.WriteLine("Finish previous task"); 
            application.EndBackgroundTask(taskId);
            taskId = 0;
        }

        taskId = application.BeginBackgroundTask(delegate {
            if (taskId != 0) {
                application.EndBackgroundTask(taskId);
                taskId = 0;
            }
        });

        Console.WriteLine("Begin task");
        ThreadPool.QueueUserWorkItem(delegate {

            application.InvokeOnMainThread(delegate() {
                for(int i = 0; i < 700; i++) {
                    if ((i + 1) % 100 == 0)
                        Console.WriteLine("Time to remain: {0} seconds", application.BackgroundTimeRemaining); 
                    //viewController.Touch();
                }

                // EndBackgroundTask must also be executed on the main thread.
                application.EndBackgroundTask(taskId);
                taskId = 0;
            });

        });
    }

关于ios - 完成 iOS 后台任务,将其移动到前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11501443/

相关文章:

c# - 子类化 UICollectionView

ios - TableView 和 Collection View 布局在同一 View Controller 中

ios - UIPopoverController 在运行 iOS 5.0.1 的 iPad 上崩溃?

ios - 足以从 super View 中删除 View 吗?

ios - 如何在初始自动旋转后修复主视图的大小?

java - 线程调度程序是 JVM 的一部分还是操作系统的一部分?

ios - Objective C 和线程

C# BackgroundWorker 并行调用 DoWorkEventHandler

.net - 是否有任何用于 Monotouch 的 OpenCV 包装器?

xamarin.ios - 如何在 Xamarin.iOS 中重定向页面