Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a Method which calls a Web Service.
C#
function void SendAsync( object input )
{
     // Log Time Before Send to WebService (T1)
     ......  Call WebMethod ....
     // Log Time After Send to WebService (T2)
}


I want to run a loop and call the Web method with different Input data. Without waiting for the response I want to keep on calling the Web Method.
C#
for ( int i=1;i<=100;i++ )
{ 
            //LOG TIME BEFORE Calling Async Method (T0)

            //TRIAL 1
            ThreadPool.QueueUserWorkItem(new WaitCallback(t.SendAsync), i.ToString());

            //TRIAL 2
            new Task(() => { SendAsync(i.ToString()); }).Start();

            //TRIAL 3
            Task.Factory.StartNew(() => SendAsync(i.ToString()));

            //TRIAL 4
            AsyncTask_Delegate adel = null;
            adel = new AsyncTask_Delegate(SendAsync);
            IAsyncResult R = null;
            R = adel.BeginInvoke(i.ToString(), null, null); //invoking the method
}

I want to call this function as Async call. I have tried various approaches as mentioned above.

The time between T0 and T1 is quite substantial (Even in 15-40 Seconds). Next Method call is happening in the loop, but the actual Call to the Web method is getting delayed. The method is not running concurrently. I even put the SendAsync method in different class and call the method still the same result.....

--------------------------------------------------------------------------------



Adding more light on the problem is that I am creating an input string during each loop and sending it to a function for processing.

Also instead of Calling a Web Method even when we call a Normal simple method, the difference between T0 and T1 is large.

The same function is running in Async mode but simply not able to run or start simultaneously.

I have tried to create a copy like "var tmp=i;" before calling, but delay is still present.
Posted
Updated 25-Aug-14 20:00pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Aug-14 3:02am    
Nothing happens "simultaneously", because order of space-like events depends on the speed of the observer. :-)
You approach is bad, enormously complicated. To get some advice, you need to explain your goal. "I want to run a loop" cannot be a goal, this is a tool of solving problem, which can be wrong. What are the goals?
—SA
jigi_chavan 26-Aug-14 8:22am    
Hi SA,
I have currently a set of Data in a DataTable.
I have a third party Web Service which requires a string formatted in particular format as input for processing.
I am currently parsing the string and sending it to Webservice on Row by Row basis.
The Web service takes some amount of time.
Instead of waiting for the response I want to keep on sending the rest of the data for processing as such maximum data can be processed in minimum time.

1 solution

I think the building of the input string is the culprit here (or possibly some other thing that's not visible in the code sample you've provided).

If you spawn of a task (be it a Task, thread pool worker or even a dedicated thread) the time between doing so and it starting to execute should be small.

Take this example program;
C#
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1 {
    class Program {
        static void Log(string message) {
            Console.WriteLine("{0:00} @ {1}; {2}", Thread.CurrentThread.ManagedThreadId, DateTime.Now, message);
        }

        static void SendAsync(object input) {
            Log(String.Format("--> SendAsync; {0}", input));
            //Thread.Sleep(500);
            Log("<-- SendAsync");
        }

        static void Invoke_1(int index) {
            ThreadPool.QueueUserWorkItem(SendAsync, index);
        }

        static void Invoke_2(int index) {
            new Task(() => SendAsync(index)).Start();
        }

        static void Invoke_3(int index) {
            Task.Factory.StartNew(() => SendAsync(index));
        }

        static void Main() {
            const int iterations = 100;

            for (var i = 0; i < iterations; ++i) {
                var index = i;
                Log(String.Format("Loop iteration {0}", index));
                //Invoke_1(index);
                Invoke_2(index);
                //Invoke_3(index);
            }

            Log("All dispatched, press any key to terminate...");
            Console.ReadKey();
        }
    }
}

It generates output that looks something like this;
09 @ 26-08-2014 09:28:04; Loop iteration 0
09 @ 26-08-2014 09:28:04; Loop iteration 1
09 @ 26-08-2014 09:28:04; Loop iteration 2
09 @ 26-08-2014 09:28:04; Loop iteration 3
11 @ 26-08-2014 09:28:04; --> SendAsync; 0
11 @ 26-08-2014 09:28:04; <-- SendAsync
10 @ 26-08-2014 09:28:04; --> SendAsync; 1
10 @ 26-08-2014 09:28:04; <-- SendAsync
09 @ 26-08-2014 09:28:04; Loop iteration 4
09 @ 26-08-2014 09:28:04; Loop iteration 5
09 @ 26-08-2014 09:28:04; Loop iteration 6
09 @ 26-08-2014 09:28:04; Loop iteration 7
09 @ 26-08-2014 09:28:04; Loop iteration 8
09 @ 26-08-2014 09:28:04; Loop iteration 9
09 @ 26-08-2014 09:28:04; Loop iteration 10
09 @ 26-08-2014 09:28:04; Loop iteration 11
09 @ 26-08-2014 09:28:04; Loop iteration 12
09 @ 26-08-2014 09:28:04; Loop iteration 13
09 @ 26-08-2014 09:28:04; Loop iteration 14
09 @ 26-08-2014 09:28:04; Loop iteration 15
09 @ 26-08-2014 09:28:04; Loop iteration 16
09 @ 26-08-2014 09:28:04; Loop iteration 17
09 @ 26-08-2014 09:28:04; Loop iteration 18
09 @ 26-08-2014 09:28:04; Loop iteration 19
09 @ 26-08-2014 09:28:04; Loop iteration 20
09 @ 26-08-2014 09:28:04; Loop iteration 21
09 @ 26-08-2014 09:28:04; Loop iteration 22
09 @ 26-08-2014 09:28:04; Loop iteration 23
09 @ 26-08-2014 09:28:04; Loop iteration 24
09 @ 26-08-2014 09:28:04; Loop iteration 25
09 @ 26-08-2014 09:28:04; Loop iteration 26
09 @ 26-08-2014 09:28:04; Loop iteration 27
09 @ 26-08-2014 09:28:04; Loop iteration 28
09 @ 26-08-2014 09:28:04; Loop iteration 29
09 @ 26-08-2014 09:28:04; Loop iteration 30
09 @ 26-08-2014 09:28:04; Loop iteration 31
09 @ 26-08-2014 09:28:04; Loop iteration 32
09 @ 26-08-2014 09:28:04; Loop iteration 33
09 @ 26-08-2014 09:28:04; Loop iteration 34
09 @ 26-08-2014 09:28:04; Loop iteration 35
09 @ 26-08-2014 09:28:04; Loop iteration 36
09 @ 26-08-2014 09:28:04; Loop iteration 37
09 @ 26-08-2014 09:28:04; Loop iteration 38
09 @ 26-08-2014 09:28:04; Loop iteration 39
09 @ 26-08-2014 09:28:04; Loop iteration 40
09 @ 26-08-2014 09:28:04; Loop iteration 41
09 @ 26-08-2014 09:28:04; Loop iteration 42
09 @ 26-08-2014 09:28:04; Loop iteration 43
09 @ 26-08-2014 09:28:04; Loop iteration 44
09 @ 26-08-2014 09:28:04; Loop iteration 45
10 @ 26-08-2014 09:28:04; --> SendAsync; 2
10 @ 26-08-2014 09:28:04; <-- SendAsync
09 @ 26-08-2014 09:28:04; Loop iteration 46

As you can see the delta time between
09 @ 26-08-2014 09:28:04; Loop iteration 0

and
11 @ 26-08-2014 09:28:04; --> SendAsync; 0

is in the sub-second range.

What's slowing your app down is either very heavy lifting involved in creating the input-string or possibly some locking going on, not the way you spawn threads.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Bernhard Hiller 26-Aug-14 9:37am    
And how do you know that the webservice he calls can return an answer immediately? Oh no, in my opinion, it is the *slow* reaction of the web service he calls which is the source of the troubles he tries to circumvent somehow.
Fredrik Bornander 26-Aug-14 9:40am    
I don't, I simply assumed because he said that even with a call to a "Normal simple" method it's slow. But I can be wrong. Either way, it's probably not caused by how he off loads the work to other threads.
jigi_chavan 27-Aug-14 0:22am    
Hi ,
I have currently a set of Data in a DataTable.
I have a third party Web Service which requires a string formatted in particular format as input for processing.
I am currently parsing the string and sending it to Webservice on Row by Row basis.
The Web service takes some amount of time.
Instead of waiting for the response I want to keep on sending the rest of the data for processing as such maximum data can be processed in minimum time.
Fredrik Bornander 27-Aug-14 3:41am    
Still not enough information to provide more than a general example (like I did above).
Show more code and more actual details if you want someone to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900