SWIG - 从 UWP 后台任务或扩展执行调用 C# [英] SWIG - C# Calling from UWP Background Task or Extended Execution

查看:34
本文介绍了SWIG - 从 UWP 后台任务或扩展执行调用 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人尝试从 UWP 后台任务、扩展执行或激活触发器中调用 C++ 库周围的 swig C# 包装器?

我的 C# swig 包装器提供用 C++ 编写的功能,用于处理对云等服务的网络调用.它可能会下载或上传文件、图像等文件,也可能只是将它们与机器上的本地副本同步.

>

我最近对 ​​UWP 应用的后台激活和前台激活主题进行了大量阅读/研究.到目前为止,我发现了什么:

  • 后台任务可以在 UWP 应用的进程内和进程外运行,但限制为 25 秒 + 5 秒的执行时间.
  • 具有应用程序触发器的后台任务限制为 10 分钟
  • 扩展执行似乎一直运行到后台任务完成
  • 或...

我的目标是能够运行此应用程序,它将启动将在后台进行同步的库(swigged C#).用户可能会也可能不会直接使用该应用程序,因此可以将应用程序最小化,并由其他应用程序(例如网络浏览器)覆盖(因此不会主动使用),但后台操作将每 10 分钟左右执行一次以进行同步.

根据我的阅读和迄今为止获得的一些巨大帮助,我正在努力寻找一条安全的道路继续前进.这3个似乎是我正在看的.我仍然不清楚哪一种最适用.

有没有人已经处理过这样的场景,可以提供一些好的建议?

解决方案

对于后台文件下载/上传场景,您可以使用 BackgroundDownloader/Uploader 类:https://docs.microsoft.com/en-us/windows/uwp/networking/background-transfers

如果您需要在后台运行长时间运行的同步/维护应用程序代码任务,而用户实际上并未运行该应用程序,请使用维护触发器.这将允许您运行长达 10 分钟,频率可高达每 15 分钟一次.但是,当设备使用电池运行时,此任务不会运行.https://docs.microsoft.com/en-us/windows/uwp/launch-resume/use-a-maintenance-trigger

对于当您的应用未在使用中时在后台进行的简单同步任务,您可以使用 TimeTrigger.这也将使用电池运行,但每次运行的执行时间限制为 30 秒(可以每 15 分钟安排一次).https://docs.microsoft.com/en-us/windows/uwp/launch-resume/run-a-background-task-on-a-timer-

如果您的应用在前台运行并在应用最小化或屏幕锁定时应继续运行的同步活动,请使用 ExtendedExecutionSession:https://docs.microsoft.com/en-us/windows/uwp/launch-resume/run-minimized-with-extended-execution

如果您的应用程序在前台并且即使用户终止应用程序也应继续运行同步活动,请触发 ApplicationTrigger 后台任务以将活动卸载到后台任务:https://docs.microsoft.com/en-us/windows/uwp/launch-resume/trigger-background-task-from-app

如果同步需要是从服务器端触发的,请查看原始推送通知以在客户端触发您的应用程序代码,即使用户没有主动运行您的应用程序:https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-raw-notification-overview

AppServices 不是这里的解决方案.AppService 是一个应用程序到应用程序的概念,其中一个应用程序提供另一个应用程序使用的服务.这似乎不是这里的情况.顺便说一句,appservice 的生命周期不是 30 秒 - 只要消费应用程序在前台,它就可以运行.但这同样不适用于您这里的场景.

I wonder if anyone has tried calling a swig C# wrapper around a C++ library from a UWP Background Task, Extended Execution, or Activation Trigger?

My C# swig wrapper provides functionality written in C++ which handles network calls to services such as cloud etc. It might download or upload files such as documents, images, etc or it might just synchronize them with local on machine copies.

I have been doing lots of reading/research lately on the topic of Background Activation and Foreground Activation from a UWP app. So far, what I have found that:

  • Background Tasks can run in-and-out-of-process of the UWP app but have limitation to 25 seconds + 5 seconds of execution time.
  • Background tasks with Application Trigger have limitation to 10 minutes
  • Extended Execution seem to run until the background task has completed
  • or...

My goal is to be able to run this application and it will initiate the library (swigged C#) which will do syncing in background. User may or may not work directly with the application, so app can be minimized, covered by another app such as web browser (so not actively used), but the background operation will execute every 10 minutes or so to do its syncing.

Based on my reading and some great help I got so far, I am trying to find a safe path to proceed. These 3 seem to be what I am looking at. Which one is most applicable is still unclear to me.

Has anyone dealt with a scenario like this already that could give some good advice?

解决方案

For file download/upload scenarios in the background you can use the BackgroundDownloader/Uploader classes: https://docs.microsoft.com/en-us/windows/uwp/networking/background-transfers

If you need to run long-running sync/maintenance application code tasks in the background without the app actually being run by the user, use a MaintenanceTrigger. This will allow you to run for up to 10min, as frequently as every 15min. However, this task will not run when the device is running on battery. https://docs.microsoft.com/en-us/windows/uwp/launch-resume/use-a-maintenance-trigger

For simple sync tasks in the background when your app is not actively in use, you can use a TimeTrigger. This will also run on battery, but is limited to 30sec execution time for each run (can be scheduled every 15min). https://docs.microsoft.com/en-us/windows/uwp/launch-resume/run-a-background-task-on-a-timer-

If your app is in the foreground and running a synchronization activity that should continue when the app gets minimized or the screen gets locked, use an ExtendedExecutionSession: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/run-minimized-with-extended-execution

If your app is in the foreground and running a synchronization activity should continue even when the user terminates the app, trigger an ApplicationTrigger background task to offload the activity to a background task: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/trigger-background-task-from-app

If the need for synchronization is a triggered from the server-side, take a look at raw push notifications to trigger your app code on the client, even when the user is not actively running your app: https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-raw-notification-overview

AppServices are not a solution here. An AppService is an app-to-app concept, where one app provides a service that another app consumes. This does not seem to be the case here. Btw, the lifetime of the appservice is not 30sec - it can run for as long as the consuming app is in the foreground. But again that's not applicable to your scenario here.

这篇关于SWIG - 从 UWP 后台任务或扩展执行调用 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆