简体   繁体   English

Windows Phone 8-在Application_Closing方法中等待HttpWebRequest完成

[英]Windows Phone 8 - waiting for HttpWebRequest to finish in Application_Closing method

I have a component in my Windows Phone 8 app (let's call it RequestSender), which contains a queue of requests, as well as a Thread (let's call it SenderThread), which reads the queue (blocks on Dequeue), and sends the requests using HttpWebRequest. 我在Windows Phone 8应用程序中有一个组件(我们将其称为RequestSender),其中包含一个请求队列,还有一个线程(我们将其称为SenderThread),该线程读取队列(出队时发生阻塞)并发送请求使用HttpWebRequest。

The HttpWebRequest inside the SenderThread's method is done in a way that simulates a synchronous call, like so: SenderThread方法中的HttpWebRequest以模拟同步调用的方式完成,如下所示:

 _requestWaitHandle.Reset();
 IAsyncResult respResult = webReq.BeginGetResponse(GetResponseStreamCallback, null);
 _requestWaitHandle.Wait();
 HttpWebResponse response = webReq.EndGetResponse(respResult) as HttpWebResponse;

The only thing which the callback method does here is setting the _requestWaitHandle (which is a ManualResetWaitHandleSlim by the way). 回调方法在这里所做的唯一一件事就是设置_requestWaitHandle(顺便说一下,它是ManualResetWaitHandleSlim)。

All this works fine, except for the situation, when I try to send a "goodbye" request when the app is being closed after the user presses the "Back" hardware button. 除情况外,所有这些都工作正常,当用户按下“后退”硬件按钮后关闭应用程序时,我尝试发送“再见”请求时。

I tried to simply enqueue the "goodbye" request in Application_Closing method, and then call a "Stop" on RequestSender. 我试图在Application_Closing方法中简单地将“再见”请求放入队列,然后在RequestSender上调用“停止”。 The Stop method would signal the thread that it should exit (by setting an appropriate flag), and then call Join() on the thread. Stop方法将向线程发出信号,通知它应该退出(通过设置适当的标志),然后在线程上调用Join()。

The SenderThread should finish processing the web request, check the "stop" flag, and simply exit. SenderThread应该完成对Web请求的处理,检查“停止”标志,然后简单地退出。 That's the theory. 那是理论。

In practice, the "goodbye" request gets enqueued, starts being processed, but the HttpWebRequest simply hangs on BeginGetResponse and never exits. 实际上,“再见”请求被排队,开始处理,但是HttpWebRequest只是挂在BeginGetResponse上而永不退出。 What may be causing this? 是什么原因造成的?

In one of the answers to the following question: Question about HttpWebRequest it's stated, that "The HttpWebRequest use UI thread to process a request, don't ask me why.". 在以下问题的答案之一中: 关于HttpWebRequest的问题指出,“ HttpWebRequest使用UI线程处理请求,不要问我为什么。”。 Is this true? 这是真的? How can I avoid this and simply make the web request? 如何避免这种情况,而只是提出Web请求? Do I need to use something else than HttpWebRequest for this? 我是否需要使用HttpWebRequest以外的其他东西?

I needed the exact same thing as you and I tested everything in the platform, but only sockets got the job done. 我需要与您完全相同的东西,并且我测试了平台中的所有内容,但是只有套接字才能完成工作。

If you want to try your luck with sockets - here's what I used (just delete the JSON-related stuff). 如果您想尝试使用套接字的运气-这就是我所使用的 (只需删除与JSON相关的内容)。 And you'll also need this library . 而且您还将需要此库

The usage is something like this: 用法是这样的:

var http = new HttpSocketConnection();
var postBody = new HttpPostBodyBuilder.*Body();
// Here you set some parameters
using (var bodyStream = postBody.PrepareData()) {
    var req = new HttpMessage.Request(bodyStream, "POST");
    req.ContentLength = bodyStream.Length;
    req.ContentType = postBody.GetContentType();

    // send request
    var response = http.Send(url, req);
}

Hope this helps. 希望这可以帮助。 :) :)

PS The code is far from perfect, but it did what I needed, and you know the old saying: "If something's not broken, don't fix it". PS该代码远非完美,但它满足了我的需要,并且您知道那句老话:“如果某些内容没有损坏,请不要修复它”。 :) :)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM