异步 WebRequest 超时 Windows Phone 7

发布于 2024-10-21 08:50:27 字数 1425 浏览 3 评论 0原文

我想知道 Windows Phone7 上 HttpWebRequest 超时的“正确”方法是什么?

我一直在阅读有关 ThreadPool.RegisterWaitForSingleObject() 的内容,但这不能用作 WaitHandles 在运行时抛出未实现的异常。

我也一直在研究 ManualReset 事件,但是 A)没有正确理解它们,B)不明白阻塞调用线程如何成为实现异步请求超时的可接受方法。

这是我现有的代码,没有超时,有人可以告诉我如何为此添加超时吗?

public static void Get(Uri requestUri, HttpResponseReceived httpResponseReceivedCallback, ICredentials credentials, object userState, bool getResponseAsString = true, bool getResponseAsBytes = false)
                {
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);

                    httpWebRequest.Method = "GET";
                    httpWebRequest.Credentials = credentials;

                    var httpClientRequestState = new JsonHttpClientRequestState(null, userState, httpResponseReceivedCallback, httpWebRequest, getResponseAsString, getResponseAsBytes);

                    httpWebRequest.BeginGetResponse(ResponseReceived, httpClientRequestState);
                }

private static void ResponseReceived(IAsyncResult asyncResult)
{
      var httpClientRequestState = asyncResult.AsyncState as JsonHttpClientRequestState;

       Debug.Assert(httpClientRequestState != null, "httpClientRequestState cannot be null. Fatal error.");

       try
       {
           var webResponse  = (HttpWebResponse)httpClientRequestState.HttpWebRequest.EndGetResponse(asyncResult);

       }
}

I'm wondering what the "right" way of timing out an HttpWebRequest is on Windows Phone7?

I've been reading about ThreadPool.RegisterWaitForSingleObject() but this can't be used as WaitHandles throw a Not implemented exception at run time.

I've also been looking at ManualReset events but A) Don't understand them properly and B) Don't understand how blocking the calling thread is an acceptable way to implement a time out on an Async request.

Here's my existing code sans timeout, can someone please show me how I would add a timeout to this?

public static void Get(Uri requestUri, HttpResponseReceived httpResponseReceivedCallback, ICredentials credentials, object userState, bool getResponseAsString = true, bool getResponseAsBytes = false)
                {
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);

                    httpWebRequest.Method = "GET";
                    httpWebRequest.Credentials = credentials;

                    var httpClientRequestState = new JsonHttpClientRequestState(null, userState, httpResponseReceivedCallback, httpWebRequest, getResponseAsString, getResponseAsBytes);

                    httpWebRequest.BeginGetResponse(ResponseReceived, httpClientRequestState);
                }

private static void ResponseReceived(IAsyncResult asyncResult)
{
      var httpClientRequestState = asyncResult.AsyncState as JsonHttpClientRequestState;

       Debug.Assert(httpClientRequestState != null, "httpClientRequestState cannot be null. Fatal error.");

       try
       {
           var webResponse  = (HttpWebResponse)httpClientRequestState.HttpWebRequest.EndGetResponse(asyncResult);

       }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀手六號 2024-10-28 08:50:27

C# 的基本异步超时模式:

public static T SafeLimex<T>(Func<T> F, int Timeout, out bool Completed)   
   {
       var iar = F.BeginInvoke(null, new object());
       if (iar.AsyncWaitHandle.WaitOne(Timeout))
       {
           Completed = true;
           return F.EndInvoke(iar);
       }
         F.EndInvoke(iar); //not calling EndInvoke will result in a memory leak
         Completed = false;
       return default(T);
   } 

不确定这是否适用于 Windows Phone 7。
您可能想看一下 -
向客户端代码公开异步功能:Windows Phone 7

Basic Async Timeout Pattern for C#:

public static T SafeLimex<T>(Func<T> F, int Timeout, out bool Completed)   
   {
       var iar = F.BeginInvoke(null, new object());
       if (iar.AsyncWaitHandle.WaitOne(Timeout))
       {
           Completed = true;
           return F.EndInvoke(iar);
       }
         F.EndInvoke(iar); //not calling EndInvoke will result in a memory leak
         Completed = false;
       return default(T);
   } 

Not sure if this will work on Windows Phone 7.
You might want to take a look at this -
Exposing asynchronous features to client code: Windows Phone 7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文