简体   繁体   English

长期运行的后台任务中的Internet访问

[英]Internet access in a long-running background task

In my Windows Phone 10 app, I've got a long-running background task, triggered by an RfcommConnectionTrigger. 在Windows Phone 10应用程序中,我有一个由RfcommConnectionTrigger触发的长期运行的后台任务。 This task will run for as long as my bluetooth Rfcomm device is connected. 只要连接了蓝牙Rfcomm设备,该任务就会运行。

Now what I want to achieve is this: 现在我要实现的是:

  • The Bluetooth device sends some data to the app on the phone (working) 蓝牙设备将一些数据发送到手机上的应用程序(正在运行)
  • The phone looks up a value using an HTTP request (not working) 手机使用HTTP请求查找值(无效)
  • The phone sends the result back to the Bluetooth device (working) 手机会将结果发送回蓝牙设备(工作中)

The problem with #2 is that as soon as I lock the device, all HTTP requests for some reason start failing. #2的问题在于,一旦我锁定了设备,由于某种原因,所有HTTP请求都会开始失败。 I'm just using the System.Net.Http.HttpClient to do a request, nothing special is going on there. 我只是在使用System.Net.Http.HttpClient发出请求,那里没有什么特别的事情。 More specifically, the exception is thrown in HttpClient.SendAsync . 更具体地说,该异常在HttpClient.SendAsync引发。

How do I get around this limitation? 如何解决此限制?

I found the solution. 我找到了解决方案。 This seems to be a completely undocumented "feature" -- you need to add a SystemConditionType.InternetAvailable condition to your background task to allow it to use internet when the device is locked. 这似乎是一个完全未记录的“功能”-您需要在后台任务中添加SystemConditionType.InternetAvailable条件,以使其在锁定设备时可以使用Internet。 I'm hoping that this doesn't mean that when the phone loses internet access, the background task is killed. 我希望这并不意味着当电话无法上网时,后台任务将被杀死。

It also means that the phone now needs to be connected to the internet in order to start the background task, which is still annoying, but I guess I can live with that. 这也意味着电话现在需要连接到互联网才能启动后台任务,这仍然很烦人,但是我想我可以接受。

暂无
暂无

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

相关问题 在后台运行一个长时间运行的并行任务,同时允许小的异步任务更新前台 - Running a long-running parallel task in the background, while allowing small async tasks to update the foreground Rx如何并行化长时间运行的任务? - Rx How to parallelize long-running task? 从ASP.NET或预定作业启动长时间运行的后台任务 - Launching a long-running background task - from ASP.NET or a scheduled job 如何将这个长期运行的后台任务转换为异步/等待? - How can I convert this long-running background task to async/await? 没有 IHostedService 运行应用程序的整个生命周期的长时间运行的任务? - Long-running task without IHostedService running the entire life of the application? 在Windows服务中运行长时间运行的任务 - Running a long-running Task within a Windows Service 在长时间运行的任务中显示自定义对话框窗口 - Displaying a custom dialog window during a long-running task 长期运行任务与线程 - 性能 - Long-running task vs. threads — performance 返回之前,先使用DBContext Access启动长时间运行的进程 - Start Long-Running Process With DBContext Access Before Returning 可以使用任务并行库一起运行长时间运行的方法和“正在工作...”对话框以允许将长时间任务写入 BindingList 吗? - Can a long-running method and a “Working…” dialog be run together using the Task Parallel Library to allow long task to write to a BindingList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM