Web请求的运行X号同时 [英] Run x number of web requests simultaneously

查看:198
本文介绍了Web请求的运行X号同时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的公司,我想在C#中发送XML文件(存储在我的驱动器上),通过我自己的的HttpWebRequest 客户端的Web服务。这已经工作。 Web服务支持在同一时间(Ⅰ得到一旦完成服务器上的处理从web服务的响应)5 synchronuous请求。加工约需5分钟,每个请求。

Our company has a web service which I want to send XML files (stored on my drive) via my own HTTPWebRequest client in C#. This already works. The web service supports 5 synchronuous requests at the same time (I get a response from the web service once the processing on the server is completed). Processing takes about 5 minutes for each request.

投掷太多的要求(> 5)导致超时为我的客户。此外,这可能会导致在服务器端和非相干数据错误。在服务器端进行更改不是一个选项(从不同的供应商)。

Throwing too many requests (> 5) results in timeouts for my client. Also, this can lead to errors on the server side and incoherent data. Making changes on the server side is not an option (from different vendor).

现在,我的的WebRequest 客户端将发送XML,等待使用 result.AsyncWaitHandle.WaitOne()的反应;

Right now, my Webrequest client will send the XML and wait for the response using result.AsyncWaitHandle.WaitOne();

不过,这种方式只有一个请求可以在同一时间内处理,虽然Web服务支持5.我尝试用的BackgroundWorker 线程池但他们在创建同一太多的要求,这使得他们对我没用。任何建议,如何人能解决这个问题?创建自己的线程池究竟有5个线程?任何建议,如何实现这一点?

However, this way, only one request can be processed at a time although the web service supports 5. I tried using a Backgroundworker and Threadpool but they create too many requests at same, which make them useless to me. Any suggestion, how one could solve this problem? Create my own Threadpool with exactly 5 threads? Any suggestions, how to implement this?

非常感谢。

碧玉

推荐答案

最简单的方法是创建5个线程(旁白:!这是一个奇数),从使用XML文件的 BlockingCollection

The easy way is to create 5 threads ( aside: that's an odd number! ) that consume the xml files from a BlockingCollection.

是这样的:

var bc = new BlockingCollection<string>();

for ( int i = 0 ; i < 5 ; i++ )
{
    new Thread( () =>
        {
            foreach ( var xml in bc.GetConsumingEnumerable() )
            {
                // do work
            }
        }
    ).Start();
}

bc.Add( xml_1 );
bc.Add( xml_2 );
...
bc.CompleteAdding(); // threads will end when queue is exhausted

这篇关于Web请求的运行X号同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆