简体   繁体   English

C#Windows Phone 8.1运行时:如何使任务在应用程序生命周期之外生存?

[英]C# Windows Phone 8.1 runtime: how to make a task survive beyond app lifecycle?

I have this kind of Task 我有这种任务

    private async Task SaveToFile(StorageFile file)
    {
        // prepare data
        await ...
        Debug.Writeline("completed");
    }

If the user press "back" button this task won't be completed. 如果用户按下“返回”按钮,则此任务将无法完成。 I need a way to make it go on until all it's done, even if the calling app is not running any more. 我需要一种使它继续进行直到完成所有操作的方法,即使调用应用程序不再运行。

No, you can not force a Task to keep running, if the parent process is terminated. 不可以,如果父进程终止,则不能强制任务继续运行。

That said, the issue you've described is not really an issue unless your file takes longer than 10 seconds to save (which is an eternity, in computer lingo). 也就是说,除非文件的保存时间超过10秒(在计算机语言中,这是永恒的),否则您所描述的问题并不是真正的问题。

Windows Phone apps continue to run in the background for up to 10 seconds, after the user 'backs' out of it, or manually quits. 用户“退出”或手动退出后,Windows Phone应用程序将继续在后台运行长达10秒钟。 This should be enough time for any application to finish its 'cleanup' operations. 对于任何应用程序来说,这应该是足够的时间来完成其“清理”操作。

See: App Lifecycle Windows Runtime Apps 请参阅: 应用程序生命周期Windows运行时应用程序

See also: App activation and deactivation for Windows Phone 8 另请参阅: Windows Phone 8的应用程序激活和停用

You have To use "background Task" for this. 您必须为此使用“后台任务” Its pretty simple, create a new project for background tasks and add it to your solution, because your background task is a not a simple class its a separate WINRT project. 这非常简单,为后台任务创建一个新项目并将其添加到您的解决方案中,因为您的后台任务不是一个简单的类,而是一个单独的WINRT项目。 To do this, right-click on your solution node in the Solution Explorer and select Add->New Project. 为此,请在解决方案资源管理器中右键单击解决方案节点,然后选择“添加”->“新建项目”。 Then select the Windows Run-time Component (Universal Windows) project type, name the project, and click OK. 然后选择Windows运行时组件(Universal Windows)项目类型,命名该项目,然后单击“确定”。 I named here "BackgroundStuff". 我在这里命名为“ BackgroundStuff”。 You can create more than one background task for single application. 您可以为单个应用程序创建多个后台任务。 there is no any limit. 没有任何限制。 Consider simple example to generate toast even App is not running: 考虑一个简单的示例,即使App未运行,也可以生成Toast:

 private async void button_Click(object sender, RoutedEventArgs e) { string myTaskName = "MyBackgroundClass"; // check if task is already registered foreach (var cur in BackgroundTaskRegistration.AllTasks) if (cur.Value.Name == myTaskName) { await (new MessageDialog("Task already registered")).ShowAsync(); return; } // Windows Phone app must call this to use trigger types (see MSDN) await BackgroundExecutionManager.RequestAccessAsync(); // register a new task BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder { Name = "MyBackgroundClass", TaskEntryPoint ="MybackgroundStuff.MyBackgroundClass" }; taskBuilder.SetTrigger(new TimeTrigger(15, true)); BackgroundTaskRegistration myFirstTask = taskBuilder.Register(); 

If you want to learn more , refer my blog: http://windowsapplife.blogspot.in/ 如果您想了解更多信息,请参阅我的博客: http : //windowsapplife.blogspot.in/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Windows Phone 8.1运行时inkmanager - C# Windows Phone 8.1 Runtime inkmanager 如何将 C# 中的 Image 对象制作为 Windows Phone 8.1 - How to make an Image object in C# to Windows Phone 8.1 如何在Windows Phone 8.1的C#中使文本块闪烁|闪烁? - How to make textblock blink|flash in C# for Windows Phone 8.1? 在Windows Phone 8.1运行时上调用外部应用程序(非Silverlight)XAML C#应用程序 - Call an external app on windows phone 8.1 runtime(not silverlight) xaml c# application 从现有的相机应用程序Windows Phone 8.1运行时C中获取图片 - get Picture from exising camera app Windows Phone 8.1 runtime c# 如何在C ++运行时组件中的Windows Phone 8.1 XAML应用程序中使用C ++ DLL - How to use a C++ dll in Windows Phone 8.1 XAML App within C++ Runtime Component 如何在Windows Phone 8.1运行时应用程序中显示计划的对话框? - How to show a scheduled dialog in a Windows Phone 8.1 Runtime app? 使用 xaml 和 c# 在 Windows Phone 8.1 中制作歌曲列表 - make songs list in Windows Phone 8.1 with xaml and c# 以编程方式在Windows Phone 8.1 C#WinRT中使WriteableBitmap透明 - Make a WriteableBitmap Transparent in Windows Phone 8.1 C# WinRT Programmatically XAML和C#WebView在Windows Phone 8.1 App中不起作用 - XAML and C# WebView not working in Windows Phone 8.1 App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM