如何在c#中一次调用多个服务 [英] how to call multiple services at a time in c#

查看:86
本文介绍了如何在c#中一次调用多个服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我试图一次拨打多个请求。



根据我发送的以下代码逐个请求,

Hi
I tried to call multiple request at a time.

as per my below code it send the request one by one,

del[f] = new delThread(fnSendandReceiveXML); // function name fnSendandReceiveXML
AsyncResult[f] = del[f].BeginInvoke(sbAuth.ToString(), strrequrl, null, null);
 while (true)
    {
    bool responserecieved = true;
    if (!(AsyncResult[f].IsCompleted))
   {
      responserecieved = false;
   }
  if (responserecieved == true)
   {
   break;
  }
}
AuthResponsedet = del[f].EndInvoke(AsyncResult[f]).ToString();




XML Request  3/7/2015 1:50:50 PM
XML Response 3/7/2015 1:50:58 PM

XML Request  3/7/2015 1:50:58 PM
XML Response 3/7/2015 1:51:06 PM

XML Request  3/7/2015 1:51:06 PM
XML Response 3/7/2015 1:51:14 PM

XML Request  3/7/2015 1:51:14 PM
XML Response 3/7/2015 1:51:22 PM

XML Request  3/7/2015 1:51:22 PM
XML Response 3/7/2015 1:51:29 PM

XML Request  3/7/2015 1:51:29 PM
XML Response 3/7/2015 1:51:37 PM

XML Request  3/7/2015 1:51:37 PM
XML Response 3/7/2015 1:51:43 PM

XML Request  3/7/2015 1:51:43 PM
XML Response 3/7/2015 1:51:50 PM



如果我们有大约40个请求那么我想一次得到回复我该怎么做请告诉我们



提前致谢



Sheethal


if we have around 40 request then i want to get the response at a time how can i do this please advise us

Thanks in advance

Sheethal

推荐答案

你的while循环阻止执行,直到它收到响应,因此它不能发送另一个请求。

你需要将它分成两个循环,然后检查所有的响应请求已经发送。

一个循环发送所有请求,然后另一个循环检查所有响应。

Your while loop is blocking execution until it receives the response, so it can't send another request.
You need to split it into two loops, and do the checking for responses after all the requests have been sent.
One loop to send all the requests, and then another to check for all the responses.
// Send all requests
for (int f = 0; f < numberOfRequests; f++)
{
    del[f] = new delThread(fnSendandReceiveXML); // function name fnSendandReceiveXML
    AsyncResult[f] = del[f].BeginInvoke(sbAuth.ToString(), strrequrl, null, null);
}

// Check for responses
bool[] responserecieved = new bool[numberOfRequests];
// Loop until all the responses have been received
while (responserecieved.Contains(false))
{
    // Loop through each request
    for (int f = 0; f < numberOfRequests; f++)
    {
        // Only check if it hasn't already been received
        if (responserecieved[f] != true)
        {
            responserecieved[f] = AsyncResult[f].IsCompleted;
            if (responserecieved[f] == true)
            {
                // Do what you want with the response here
                AuthResponsedet = del[f].EndInvoke(AsyncResult[f]).ToString();
            }
        }
    }
}


这篇关于如何在c#中一次调用多个服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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