调试WinRT的后台网络

时间:2012-08-03 08:48:34

标签: c# debugging sockets background windows-runtime

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

尝试了一切来调试后台任务中的代码,没有用:

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

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

1 个答案:

答案 0 :(得分:1)

默认情况下,引用的项目不会添加到主项目中。这看起来并不那么明显,这就是为什么我花了差不多一个星期才发现这一点。 所以线索是:检查参考项目的可访问性

UPD:

还有一些事情要处理,正如我在开发过程中发现的那样。其中一些并不像他们需要的那样清晰。以下列出了我所做的事情:

  1. 将后台项目添加到主项目的引用中(右键单击解决方案浏览器中的引用节点)。
  2. 检查主项目清单是否包含正确的声明(后台任务w /控制通道,右后台入口点名称为完整包,$ targetnametoken $ .exe为可执行文件)
  3. 从#1开始的事情:您计划在后台使用的所有实体应该被放入解决方案中的单独项目中。然后,主项目和后台项目都会引用该项目。
  4. 注意在注册ControlChannelTrigger之前要调用的BackgroundExecutionManager.RequestAccessAsync()
  5. 我在一个示例项目的一个小评论中找到了一个关键的东西:
  6.             // 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线程(特别是等待的东西) - 在app运行之前它们不会运行,并且会等待。