1

我制作了一个项目,而不是使用 tcp 套接字连接(自己的封闭协议),添加了与网络触发 API 的后台连接,如此所述(从第 17 页开始) - StreamSocket 控制通道注册块和 IBackgroundTask 类,每个都应该触发时间套接字接收一些东西。

已尝试一切在后台任务中调试代码,但没有用:

  • 用手势关闭可见的应用程序
  • 锁定屏幕
  • 试图加载其他一些繁重的应用程序,让 Windows 暂停我的应用程序

所有这些都没有帮助我在套接字消息期间使后台任务运行(和调试)。我究竟做错了什么?我是否必须获得单独的可暂停设备(如 WinRT 平板电脑)才能使其正常工作?

4

1 回答 1

1

默认情况下,引用的项目不会添加到主项目中。这并不像看起来那么明显,这就是为什么我花了将近一周的时间来找出这个。所以线索是:检查参考项目可访问性

更新:

正如我在开发过程中发现的那样,还有更多的事情需要处理。其中一些并不像他们需要的那样清楚。这是我所做的列表:

  1. 将后台项目添加到主项目的引用中(右键单击解决方案浏览器中的引用节点)。
  2. 检查主项目清单是否包含正确的声明(带控制通道的后台任务,带有完整包的正确后台入口点名称,$targetnametoken$.exe 作为可执行文件)
  3. 从 #1 引出的一件事:您计划在后台使用的所有实体都应该放在解决方案中的单独项目中。然后这个项目被主项目和后台项目引用。
  4. 请注意在注册 ControlChannelTrigger 之前要调用 BackgroundExecutionManager.RequestAccessAsync()
  5. 我在示例项目的一个小评论中发现了一件关键的事情:
            // IMPORTANT: When using winRT based transports such as StreamWebSocket with the ControlChannelTrigger, 
            // we have to use the raw async pattern for handling reads instead of the await model.  
            // Using the raw async pattern allows Windows to synchronize the PushNotification task's  
            // IBackgroundTask::Run method with the return of the receive  completion callback.  
            // The Run method is invoked after the completion callback returns. This ensures that the app has 
            // received the data/errors before the Run method is invoked. 
            // It is important to note that the app has to post another read before it returns control from the completion callback. 
            // It is also important to note that the DataReader is not directly used with the  
            // StreamWebSocket transport since that breaks the synchronization described above. 
            // It is not supported to use DataReader's LoadAsync method directly on top of the transport. Instead, 
            // the IBuffer returned by the transport's  ReadAsync method can be later passed to DataReader::FromBuffer() 
            // for further processing. 

更多信息在这里 - http://code.msdn.microsoft.com/windowsapps/ControlChannelTrigger-91f6bed8/sourcecode?fileId=57961&pathId=2085431229

如果你做的一切都正确,后台任务的调试就很简单了。只需设置断点并继续,不要管主项目正在运行或暂停。

ps - 如果项目被挂起,请注意调用 UI 线程(尤其是等待的东西) - 它们在应用程序运行之前不会运行,并且会等待。

于 2012-08-06T14:30:29.643 回答