- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想使用 BeginGetResponse 方法来调用列表中的许多 URL。我有 2 个关于如何实现这个的问题:
我们使用:
public static ManualResetEvent allDone= new ManualResetEvent(false);
在 Web 应用程序中使用静态成员是否明智,因为它与其他线程共享?这会导致问题吗?
谢谢
最佳答案
虽然您可以使用事件,但我建议您使用 Task<T>
和 FromAsync
method在 TaskFactory
class 上像这样:
// Execution of tasks starts here because of the
// call to ToArray.
Task<WebResponse>[] tasks = uris.Select(u => {
// Create the request.
WebRequest req = ...;
// Make the call to return the response asynchronously with
// a Task.
return Task.Factory.FromAsync(req.BeginGetResponse,
req.EndGetResponse, null);
}).ToArray();
一旦有了它,您就可以轻松地等待所有 Task<T>
使用 ContinueWhenAll
method 的实例在 TaskFactory
上像这样继续上课:
Task.Factory.ContinueWhenAll(tasks, t => {
// Note that t is an array of Task, so you have to cast
// each element to a Task<WebRequest>.
// Process all of them here.
});
注意上面返回一个 Task
完成后您将不得不等待或继续(如果您担心通知)。
如果您使用的是 .NET 4.5,则不需要使用 ContinueWhenAll
TaskFactory
上的方法类,但可以使用 WhenAll
method在 Task
上执行工作的类:
// Note that work does not start here yet because of deferred execution.
// If you want it to start here, you can call ToArray like above.
IEnumerable<Task<WebResponse>> tasks = uris.Select(u => {
// Create the request.
WebRequest req = ...;
// Make the call to return the response asynchronously with
// a Task.
return Task.Factory.FromAsync(req.BeginGetResponse,
req.EndGetResponse, null);
});
// Execution will start at this call:
Task<Task<WebRequest>[]> allTasks = Task.WhenAll(tasks);
// Continue or wait here.
关于c# - Web 应用程序中的 BeginGetResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420252/
我需要对 Web 资源发出异步请求并使用此页面 (link to full example) 中的示例: HttpWebRequest myHttpWebRequest= (HttpWebReques
这是我的代码: namespace RequestApi { public partial class MainPage : PhoneApplicationPage {
因此,我正在创建一个使用HTTP API来获取约50,000个帐户中每个帐户状态的应用程序。该代码循环遍历每个帐户,并为其发送HTTP请求。当我根据某些条件收到http请求的响应时,将创建一个新线程来
我无法按照示例获取回调。 我有以下代码: private void startWebRequest(object sender, EventArgs e) { Uri url
我在任务工厂内部调用 httpwebresponse,执行 1 次后抛出异常 无法在上一个调用仍在进行时重新调用 BeginGetRequestStream/BeginGetResponse 我的代码
我想使用 BeginGetResponse 方法来调用列表中的许多 URL。我有 2 个关于如何实现这个的问题: 根据 http://msdn.microsoft.com/en-us/library/
我遇到这样一种情况,我需要通过 Web 请求发送自定义值,以便我可以在我的应用程序收到的响应中使用它。因为我的应用程序可能会发送多个请求,所以我正在使用 BeginGetResponse异步发送每个请
我正在尝试异步发出网络请求。我的代码工作正常,除了一件事:似乎没有内置方法可以在 BeginGetResponse 上指定超时。 MSDN 示例清楚地显示了一个工作示例,但其缺点是它们最终都以 Som
我正在编写一个 Windows Phone 8 应用程序,它应该向服务器发送 GET+POST 请求并解析答案。 我用来发送请求和获取响应的代码如下(它写在一个单独的静态类中):
我再次遇到 PCL(可移植类库)中的 WebRequest 问题。 我有这段代码: static ManualResetEvent allDone = new ManualResetEvent
我正在使用 HttpWebRequest 调用网络服务。如果 BeginGetResponse 的 AsyncCallback 抛出错误,我想将它传播到我的主程序流。我在执行此操作时遇到了问题,因为错
HttpWebRequest.BeginGetResponse 不遵守 HttpWebRequest 的任何超时属性(Timeout 或 ReadWriteTimeout)。 我阅读了一些方法来获得相
我正在高负载下测试 ASP.NET (.NET 4) 网络应用程序,发现在某些情况下 HttpWebRequest.BeginGetResponse() 会同步完成,不会抛出任何异常。 在高负载下在多
我有以下代码: private void GetRequestStreamCallback(IAsyncResult asynchronousResult) {
我在我的代码调用中使用 HttpWebRequest.BeginGetResponse() 方法从我的服务器获取数据。服务器生成的内容范围可能从几 KB 到几 GB。 我的问题是 HttpWebReq
我想知道这两种方法中哪种方法更好?我需要创建一个 200 毫秒到 5 秒之间的 Web 请求。我需要 html 响应才能继续 - 所以需要在主线程上阻塞。 第一种方法 string GetRespon
我正在使用一个使用 web 服务来推送一些事件的客户端 - web 服务的设计是这样的,即在客户端发布订阅命令时,它会发回一些感兴趣的事件并继续这样做只要客户端保持连接。 当 POST 命令时,服务会
目前在我们的一个应用程序中,我发现在执行第一个 HTTP Webrequest 时有很大的滞后。根据我们的日志,有 30 - 60 秒的延迟。它在 HttpWebRequest.BeginGetRes
我正在尝试异步获取 HttpWebResponse 对象,以便允许取消请求(使用 BackgroundWorker.CancellationPending 标志)。但是,下面的 request.Beg
我想在 UI 上显示下载正在进行时下载文件。使用 WebRequest 我有两个选择: 使用 WebRequest.BeginGetResponse 和相关方法。 优点:可以以字节为单位显示准确的进度
我是一名优秀的程序员,十分优秀!