473,320 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Threads asynchronous, web service synchronous?

I create and start several threads, each thread executes the same method -
within the method, a web service is invoked. I find that the more threads I
use, the longer it takes for all of the threads to finish. The threads are
asynchronous, correct? And I thought each would be able to use it's own
version of the web service. Am I wrong, does the web service force only one
thread at a time to execute?

Here is code if interested... I'm using NUnit for it.

static Int32 _threadFinished = 0;

[Test]
public void SimulateManyUsers()
{
Int32 howManyUsers = 10;
for (Int32 x = 0; x < howManyUsers; x++)
{
Thread t = new Thread(new ThreadStart(this.UseService));
t.Start();
}

//Give threads a chance to finish
Thread.Sleep(20000);

//Check the results
Assertion.AssertEquals("One or more of the threads did not
finish.",howManyUsers,_threadFinished);

}

private void UseService()
{
DateTime refDate = new DateTime(1998,07,04);
DateTime respDate = new DateTime(2003,06,28);
ArmCalcParameters[] i = new ArmCalcParameters[25];
for (Int32 x = 0; x< i.Length; x++)
{
i[x] = new ArmCalcParameters();
i[x].SR = "16";
i[x].SRMP = 12.32f;
i[x].ReferenceDate = refDate;
i[x].ResponseDate = respDate;
i[x].CalcType = CalculationType.SRMPtoARM;
i[x].TransId = x.ToString();
}

ArmCalcParametersOut[] o = new ArmCalcParametersOut[i.Length];
ArmCalcService svc = new ArmCalcService();
o = svc.CalculateBatch(i);
_threadFinished++;
}
Nov 16 '05 #1
5 4799
Hi Marty:

The more threads you add, the more load there is on your machine and
on the machine with the web service, so some amount of additional time
is expected for each thread you've added. The web service will not
force single threaded execution (unless there is code in the web
service call to force single threaded execution).

Couple observations:

Use Interlocked.Increment(x) from System.Threading for a thread safe
increment on the static variable instead of the postfix increment
operator (x++).

If you use an array of Thread in your program, you could call the Join
method on each Thread instance you've started to make sure the thread
completes its work and terminates.
Hope this helps,

--
Scott
http://www.OdeToCode.com

On Tue, 27 Apr 2004 15:48:59 -0700, "Marty McDonald"
<mc******@wsdot.wa.gov> wrote:
I create and start several threads, each thread executes the same method -
within the method, a web service is invoked. I find that the more threads I
use, the longer it takes for all of the threads to finish. The threads are
asynchronous, correct? And I thought each would be able to use it's own
version of the web service. Am I wrong, does the web service force only one
thread at a time to execute?

Here is code if interested... I'm using NUnit for it.

static Int32 _threadFinished = 0;

[Test]
public void SimulateManyUsers()
{
Int32 howManyUsers = 10;
for (Int32 x = 0; x < howManyUsers; x++)
{
Thread t = new Thread(new ThreadStart(this.UseService));
t.Start();
}

//Give threads a chance to finish
Thread.Sleep(20000);

//Check the results
Assertion.AssertEquals("One or more of the threads did not
finish.",howManyUsers,_threadFinished);

}

private void UseService()
{
DateTime refDate = new DateTime(1998,07,04);
DateTime respDate = new DateTime(2003,06,28);
ArmCalcParameters[] i = new ArmCalcParameters[25];
for (Int32 x = 0; x< i.Length; x++)
{
i[x] = new ArmCalcParameters();
i[x].SR = "16";
i[x].SRMP = 12.32f;
i[x].ReferenceDate = refDate;
i[x].ResponseDate = respDate;
i[x].CalcType = CalculationType.SRMPtoARM;
i[x].TransId = x.ToString();
}

ArmCalcParametersOut[] o = new ArmCalcParametersOut[i.Length];
ArmCalcService svc = new ArmCalcService();
o = svc.CalculateBatch(i);
_threadFinished++;
}


Nov 16 '05 #2
Oh, just remember the Increment method of Interlocked takes a ref of
course, otherwise it would be rather useless :)

Interlocked.Increment(ref x)

--s

--
Scott
http://www.OdeToCode.com
Nov 16 '05 #3
Hi Marty,

As Scott has mentioned, the ASP.NET webservice won't force only one thread
to service the coming request. How it works is actually the same as how the
ASP.NET web application servcie the web request. It'll use a certain
workerthread in the threadpool to service the coming request. And if multi
requests come at the same time, and the existing pooling threads not
enought, new workerthread will be created and also, there is a
"maxWorkerThreads" in the asp.net's <processModel> setting (in the
machine.config).
#<processModel> element
http://msdn.microsoft.com/library/en...cessmodelSecti
on.asp?frame=true

When the comming requests reach this limitation, new coming request will be
queued until the preceding ones are finished.

Also, in addition to use multi-threads to call webservice asynthronous, we
can also use the SoapHttpClient class's buildin async support to call
webmethod as asynthronous mode. Such as BeginXXX and EndXXX. Here is a
certain tech article discussing on this:

#Asynchronous Web Service Calls over HTTP with the .NET Framework
http://msdn.microsoft.com/library/en...09032002.asp?f
rame=true

Also, all these approachs are performing asynthronized processing at
clientside. There are also something we can do to perform asynthronous at
serverside. Below is another tech article discussing on this:

#Server-Side Asynchronous Web Methods
http://msdn.microsoft.com/library/en...10012002.asp?f
rame=true

Further more, I recommend that you perform a simple test to confirm this.
For example, provide such as webmethod which will sleep for serveral
seconds and return the threadId and serverside time to client:
public WSResult WaitingFor(int ms)
{
System.Threading.Thread.Sleep(ms);

WSResult res = new WSResult();
res.ThreadID = AppDomain.GetCurrentThreadId().ToString();
res.TimeTicket = DateTime.Now.ToLongTimeString();

return res;
}

public class WSResult
{
public string ThreadID = "";
public string TimeTicket = "";
}

Then, you can try calling this one via both the following means:
1. Call the webmethod in multthreads synthronously ,just as you do
originally.

2. Call the webmethod asynthronously multi-times in single thread( in a
click event)

You can list all the return results in a listbox to have a look. I've
tested on my side and it confirm that the serverside will use multi
workerthread to processing multi comming request.
Hope all these help. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx




Nov 16 '05 #4
"Marty McDonald" <mc******@wsdot.wa.gov> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I create and start several threads, each thread executes the same method -
within the method, a web service is invoked. I find that the more threads I use, the longer it takes for all of the threads to finish. The threads are asynchronous, correct? And I thought each would be able to use it's own
version of the web service. Am I wrong, does the web service force only one thread at a time to execute?


There are a couple things that may cause the asynchronous calls to be queued
up and not execute concurrently.

First, there is client-side limit of at most two simultaneous calls. This is
due to HTTP 1.1 protocol that says that one client should have at most two
HTTP calls in progress to one server. You can change this limit e.g., by
modifying the maxconnection attribute in the connectionManagement element in
application config file.

Second, if the Web service uses ASP.NET session state, then ASP.NET will
serialize calls from one client.

Finally, if the server has no free worker threads, then further requests
will be queued up until a thread becomes available. This is controlled with
the maxWorkerThreads setting.

Regards,
Sami
Nov 16 '05 #5
Thanks to all of you who have replied to my post. All of your information
is very useful and informative! Right now, the client and web service are
both on my machine. I will certainly include your suggestions in my code.
The whole purpose of this is to test the responsiveness of my web service.
I also think that it is better if I test this when the web service is on
another server, which is closer to what it will look like in production.
Thank you all!
--Marty
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ondrej Sevecek | last post by:
Hello, what is the difference between "event" and the only use of delegate? Why one should note events with "event" keyword when the functionality seems the same as with pure delegates? When I...
11
by: Charles Law | last post by:
I know I have brought this one up before, but I didn't get an answer last time, so hopefully I will have better luck this time. I send data out of a serial port on my main thread. I wait for a...
1
by: Alexander Kaplunov | last post by:
I have two different scenarios: 1. I have an application that calls a web service and passes an object. Web Service takes that object and does some stuff with it, which could take...
4
by: Ryan Liu | last post by:
TcpClient has a method called GetworkStream GetStream(); So in other words, there is only one stream associate with it for input and output, right? So while it is receiving, it can not send, and...
0
by: Ken T. | last post by:
I have questions regarding what happens on the web server side during an asynchronous web method call when the client aborts its request via .Abort() call. Background: My client app...
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
1
by: jan.loucka | last post by:
I'm developing WinForms application in .NET 2.0 that talks to web service. The automatically generated proxy (reference class) has got methods for both synchronous and asynchronous invocations of...
1
by: jan.loucka | last post by:
I'm developing WinForms application in .NET 2.0 that talks to web service. The automatically generated proxy (reference class) has got methods for both synchronous and asynchronous invocations of...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.