WebRequest.BeginGetResponse 不能异步工作

发布于 2024-12-05 09:54:48 字数 1214 浏览 2 评论 0原文

我有 C# 代码,并且遇到 webRequest.begingetresponse 问题。

当我尝试将其用于异步调用时,工作线程被阻塞并等待回调被调用。但是,正如我在文档中读到的那样,当前线程应该继续运行,并且一旦从服务器返回响应,回调方法应该由不同的线程调用。

[更新]实际上从服务器获取响应是当前线程被阻塞的地方,当我检查线程ID时,调用回调的线程与发送请求的线程是同一个线程。

知道我可能会错过什么吗?

这是代码片段:

public class MyRequestState
{

    public WebRequest Request;
    public WebResponse Response;
    public ManualResetEvent allDone = new ManualResetEvent(false);

    public MyRequestState()
    {

        Request = null;
        Response = null;
    }
}


public class SendRequest
{
    private void ResponseCallback(IAsyncResult result)
    { 
     //do sth ... 
     state.Response = (HttpWebResponse)request.EndGetResponse(result);
     //do sth ...
     state.allDone.Set();
    }
    public void MakeWebRequestAsync(string url)
    {
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
     request.Method = "GET";
     request.Proxy = null;
     state state = new MyRequestState();
     state.Request = request;

     IAsyncResult result = request.BeginGetResponse(new    System.AsyncCallback(ResponseCallback), state);

     state.allDone.WaitOne();
    }

}

I have C# code and have a problem with webRequest.begingetresponse.

When I try to used it for asynchronous call, the working thread is blocked and waits for the callback to be called. But, as I read in documentation, the current thread should continue to run and the callback method should be invoked by a different thread once the response is back from the server.

[UPDATE] Actually getting the response back from the server is where the current thread is blocked, and when I check the thread IDs, the thread which calls the callback is the same thread who sent the request.

Any idea what I might be missing?

Here's a snippet of the code:

public class MyRequestState
{

    public WebRequest Request;
    public WebResponse Response;
    public ManualResetEvent allDone = new ManualResetEvent(false);

    public MyRequestState()
    {

        Request = null;
        Response = null;
    }
}


public class SendRequest
{
    private void ResponseCallback(IAsyncResult result)
    { 
     //do sth ... 
     state.Response = (HttpWebResponse)request.EndGetResponse(result);
     //do sth ...
     state.allDone.Set();
    }
    public void MakeWebRequestAsync(string url)
    {
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
     request.Method = "GET";
     request.Proxy = null;
     state state = new MyRequestState();
     state.Request = request;

     IAsyncResult result = request.BeginGetResponse(new    System.AsyncCallback(ResponseCallback), state);

     state.allDone.WaitOne();
    }

}

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

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

发布评论

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

评论(2

幼儿园老大 2024-12-12 09:54:48

WebRequest.BeginGetResponse 同步等待连接,然后异步接收数据。如果连接需要一些时间,它会冻结调用线程一段时间。

WebRequest.BeginGetResponse wait for connection synchronously and then receive data asynchronously. If the connection takes some times, it will freeze the calling thread for a while.

旧时浪漫 2024-12-12 09:54:48

您在启动请求后立即等待 ManualResetEvent。这就是你的线程阻塞的原因。在调用完成的回调之前,不会通知线程继续。

You are waiting on your ManualResetEvent right after starting the request. That's why your thread blocks. The thread isn't being signaled to continue until your completed callback is called.

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