我做了一个项目而不是使用tcp套接字连接(自己的封闭协议),添加了与网络触发器API的后台连接,如here所述(从第17页开始) - StreamSocket控制通道注册块和IBackgroundTask class,每次socket接收时都应该触发。
尝试了一切来调试后台任务中的代码,没有用:
所有这些都没有帮助我在套接字消息期间运行后台任务(和调试)。我究竟做错了什么?我是否应该使用单独的可挂起设备(如WinRT平板电脑)才能使其正常工作?
答案 0 :(得分:1)
默认情况下,引用的项目不会添加到主项目中。这看起来并不那么明显,这就是为什么我花了差不多一个星期才发现这一点。 所以线索是:检查参考项目的可访问性。
UPD:
还有一些事情要处理,正如我在开发过程中发现的那样。其中一些并不像他们需要的那样清晰。以下列出了我所做的事情:
// 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.
如果你做了所有事情,后台任务的调试很简单。只要把断点继续下去,永远不要主要项目正在运行或暂停。
ps - 如果项目被暂停,请注意调用UI线程(特别是等待的东西) - 在app运行之前它们不会运行,并且会等待。