473,651 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Asynchronous Programming

I'm trying to determine if I need to make my application multi-threaded, or
if it can be done with asynchronous programming. I realize that asynch calls
do create a new thread in the background, but when they come back, they
return to the thread that launched them, is that correct? If I go
multi-threaded, then I just need to spawn a new thread for each new
connection, right?

What I'm doing here is making a service that accepts connections via TCP,
does some stuff, and then returns a response. Would my best bet be to go
asych or multi-threaded? Any articles on either or both would be much
appreciated.

Thanks,
Steve
Nov 15 '05 #1
48 5411
"Steve - DND" <steve!@!digita lnothing.com> wrote in message
news:O6******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm trying to determine if I need to make my application multi-threaded, or if it can be done with asynchronous programming. I realize that asynch calls do create a new thread in the background, but when they come back, they
return to the thread that launched them, is that correct? If I go
multi-threaded, then I just need to spawn a new thread for each new
connection, right?

What I'm doing here is making a service that accepts connections via TCP,
does some stuff, and then returns a response. Would my best bet be to go
asych or multi-threaded? Any articles on either or both would be much
appreciated.

Thanks,
Steve


Here's my favorite doc regarding threading in .NET:

http://www.eps-software.com/download..._threading.pdf

Erik
Nov 15 '05 #2
"Steve - DND" <steve!@!digita lnothing.com> wrote in
news:O6******** ******@TK2MSFTN GP09.phx.gbl:
I'm trying to determine if I need to make my application multi-threaded,
or if it can be done with asynchronous programming. I realize that
asynch calls do create a new thread in the background, but when they
come back, they return to the thread that launched them, is that
correct? If I go multi-threaded, then I just need to spawn a new thread
for each new connection, right?
My advice is unless its just one isolated task - such as a single file read
or other, thread it. Your code will be much cleaner.
What I'm doing here is making a service that accepts connections via
TCP, does some stuff, and then returns a response. Would my best bet be
to go asych or multi-threaded? Any articles on either or both would be
much appreciated.


Multi threaded. Your code will be much cleaner, and more maintainable.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #3

"Erik Frey" <er*******@hotm ail.com> wrote in message
news:eg******** ******@TK2MSFTN GP11.phx.gbl...
"Steve - DND" <steve!@!digita lnothing.com> wrote in message
news:O6******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm trying to determine if I need to make my application multi-threaded,

or
if it can be done with asynchronous programming. I realize that asynch

calls
do create a new thread in the background, but when they come back, they
return to the thread that launched them, is that correct? If I go
multi-threaded, then I just need to spawn a new thread for each new
connection, right?

What I'm doing here is making a service that accepts connections via TCP, does some stuff, and then returns a response. Would my best bet be to go
asych or multi-threaded? Any articles on either or both would be much
appreciated.

Thanks,
Steve


Here's my favorite doc regarding threading in .NET:

http://www.eps-software.com/download..._threading.pdf


One thing I have a question on is scalability performance. I keep reading
that a multi-threaded approach may not necessarily scale very well. How many
threads is too many before the switching starts to kill the app? I will
probably have about 50-75(possibly up to 100) concurrent connections at peak
times. From what I have read, it sounds like this would be too many threads
to create.
Nov 15 '05 #4
Steve,

Check out the ThreadPool Class. It'll handle multiple threads for you
in a much cleaner manner.

Chris R.

"Steve - DND" <steve!@!digita lnothing.com> wrote in message
news:ux******** ******@TK2MSFTN GP09.phx.gbl...

"Erik Frey" <er*******@hotm ail.com> wrote in message
news:eg******** ******@TK2MSFTN GP11.phx.gbl...
"Steve - DND" <steve!@!digita lnothing.com> wrote in message
news:O6******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm trying to determine if I need to make my application multi-threaded,
or
if it can be done with asynchronous programming. I realize that asynch calls
do create a new thread in the background, but when they come back,
they return to the thread that launched them, is that correct? If I go
multi-threaded, then I just need to spawn a new thread for each new
connection, right?

What I'm doing here is making a service that accepts connections via

TCP, does some stuff, and then returns a response. Would my best bet be to go asych or multi-threaded? Any articles on either or both would be much
appreciated.

Thanks,
Steve


Here's my favorite doc regarding threading in .NET:

http://www.eps-software.com/download..._threading.pdf


One thing I have a question on is scalability performance. I keep reading
that a multi-threaded approach may not necessarily scale very well. How

many threads is too many before the switching starts to kill the app? I will
probably have about 50-75(possibly up to 100) concurrent connections at peak times. From what I have read, it sounds like this would be too many threads to create.

Nov 15 '05 #5
A good way to go would be to wait on multiple socket events (one socket for
each tcp client) in a WaitForMultiple Events call, then get the index of the
event and look that up to get your client socket object. You need to use
the win32 WSAEventSelect( ) method to make the sockets non-blocking and
associate and event with the socket - .NET does not currently support this
in managed code. However, after you do that, you should be able to use the
standard socket and TCPclient send and rec methods as normal. If you will
have many concurrent clients with concurrent send and receives, then using
one thread per client is not really optimal in server design and will be
slower then other designs because of the thread context switches and
contention for shared resources goes up with number of threads.. If many
clients, you probably want to go async. You could probably also use I/O
completion ports and a thread pool, have not really seen good examples of
this from managed code however.
--
William Stacey, MVP
--
William Stacey, MVP

"Steve - DND" <steve!@!digita lnothing.com> wrote in message
news:O6******** ******@TK2MSFTN GP09.phx.gbl...
I'm trying to determine if I need to make my application multi-threaded, or if it can be done with asynchronous programming. I realize that asynch calls do create a new thread in the background, but when they come back, they
return to the thread that launched them, is that correct? If I go
multi-threaded, then I just need to spawn a new thread for each new
connection, right?

What I'm doing here is making a service that accepts connections via TCP,
does some stuff, and then returns a response. Would my best bet be to go
asych or multi-threaded? Any articles on either or both would be much
appreciated.

Thanks,
Steve

Nov 15 '05 #6
I would agree. If all those clients are ~active, your going to spend a
larger respective amount of time context switching and competing for locks,
etc. then actually working. Each thread also gets a smaller quantum time to
do work as more threads are running in the system. So less time each thread
has to work, more context switch time, more contention for locks, etc. I am
not sure the Async read and writes using standard Socket or TCPClient are
much better. AFAICT, if you post reads for all those sockets, it will still
use that many threads from the pool to run the delegates for replies, not
much better as you end up hurding threads and your still using one thread
per client per read/write afaict. And does it queue the Async Reads if the
thread pool runs out of threads (i.e. more then 25) and block on new Async
requests or queue them and let the 25 threads work on the queue? Not sure.
There is some gray area here that would need to be tested. The overhead of
using the system supplied Async abstration is not free either. There a lot
of setup and tear down expense. Doing some informal testing on a circular
queue with one producer and one consumer I get about this each time:
Custom Async get/put: 00:00:10.359375 0
Sync get/put: 00:00:01.093750 0
CLR Async get/put: 00:00:37.625000 0

The System async infrastructor is about 3-4 times slower then doing async
yourself and service the queue with one thread running in a tight loop. The
clr async is really easy, however, and seems to work well.

If perf is key, you may have to resort to overlapped io sockets or
WSAEventSelect and wrap a few pInvoke apis.

--
William Stacey, MVP

"Steve - DND" <steve!@!digita lnothing.com> wrote in message
news:ux******** ******@TK2MSFTN GP09.phx.gbl...

"Erik Frey" <er*******@hotm ail.com> wrote in message
news:eg******** ******@TK2MSFTN GP11.phx.gbl...
"Steve - DND" <steve!@!digita lnothing.com> wrote in message
news:O6******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm trying to determine if I need to make my application multi-threaded,
or
if it can be done with asynchronous programming. I realize that asynch calls
do create a new thread in the background, but when they come back,
they return to the thread that launched them, is that correct? If I go
multi-threaded, then I just need to spawn a new thread for each new
connection, right?

What I'm doing here is making a service that accepts connections via

TCP, does some stuff, and then returns a response. Would my best bet be to go asych or multi-threaded? Any articles on either or both would be much
appreciated.

Thanks,
Steve


Here's my favorite doc regarding threading in .NET:

http://www.eps-software.com/download..._threading.pdf


One thing I have a question on is scalability performance. I keep reading
that a multi-threaded approach may not necessarily scale very well. How

many threads is too many before the switching starts to kill the app? I will
probably have about 50-75(possibly up to 100) concurrent connections at peak times. From what I have read, it sounds like this would be too many threads to create.

Nov 15 '05 #7
"Steve - DND" <steve!@!digita lnothing.com> wrote in
news:ux******** ******@TK2MSFTN GP09.phx.gbl:
One thing I have a question on is scalability performance. I keep
reading that a multi-threaded approach may not necessarily scale very
well. How many threads is too many before the switching starts to kill
the app? I will probably have about 50-75(possibly up to 100) concurrent
connections at peak times. From what I have read, it sounds like this
would be too many threads to create.


Ill post more later - but 100 thread is NOTHING. Dont worry till you get near
500 or more... Even 1000 is ok in most circumstances.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #8
"William Stacey [MVP]" <st***********@ mvps.org> wrote in
news:eK******** ******@TK2MSFTN GP09.phx.gbl:
I would agree. If all those clients are ~active, your going to spend a
larger respective amount of time context switching and competing for
locks, etc. then actually working. Each thread also gets a smaller
quantum time to do work as more threads are running in the system. So


Actually thats not correct - its a common misconception. I have to go out to
pay some bills but will post more on this later. Or check your copy of Indy
in Depth for details about this...

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #9
Lay it on me brother :-) As a side note, was not talking about Indy, but
..Net. If the Socket classes BeginX methods do not use the Std Async tech
with the thread pool, I would be interested in that and appreciate the
correction. Thanks!

--
William Stacey, MVP

"Chad Z. Hower aka Kudzu" <cp**@hower.org > wrote in message
news:Xn******** **********@127. 0.0.1...
"William Stacey [MVP]" <st***********@ mvps.org> wrote in
news:eK******** ******@TK2MSFTN GP09.phx.gbl:
I would agree. If all those clients are ~active, your going to spend a
larger respective amount of time context switching and competing for
locks, etc. then actually working. Each thread also gets a smaller
quantum time to do work as more threads are running in the system. So
Actually thats not correct - its a common misconception. I have to go out

to pay some bills but will post more on this later. Or check your copy of Indy in Depth for details about this...

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Nov 15 '05 #10

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

Similar topics

6
3478
by: ... | last post by:
Does anyone know a good tutorial on asynchronous programming in .net AsyncCallback And IASyncResult are driving me crazy. And the msdn documentation is not really helpful on this topic I appreciate any recommendation.
4
1561
by: Chris | last post by:
Hello, With asynchronous programming : Why does the callback-function (running in a worker thread) may not update the state of a control on the main-form ? The docs say that you must use a MethodInvoker instead as in following example :
9
8666
by: Michael Lindsey | last post by:
I need to write a server app to send images to client GUIs that are outside of the server's domain. The client will have the file system path to the image but can not access the file system. I am trying to decide if I should use remoting vs. writing a server that uses networkstreams. I have read that networkstreams\tcp programming should be faster than remoting and is a better choice for what I am doing but that it is difficult to code.
4
348
by: bernardpace | last post by:
Hi, I am trying to get more familiar with asynchronous programming. I was reading through the document found on the page: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpovrasynchronousprogrammingoverview.asp Now, I read that 'Always call EndInvoke after your asynchronous call completes'.
1
1545
by: Julian Hershel | last post by:
Reading about asynchronous programming (ms-help://MS.NETFrameworkSDK/cpguidenf/html/cpconasynchronousdesignpatterno verview.htm) I could not clarify some doubts. Hope you can help me. 1) Are asynchronous programming and multithreaded programming two different pictures? As I read the help topic above it is not clear to me if the design pattern opens a new thread or not to run the methods asynchronously. 2) One unique thread can run...
1
3165
by: org | last post by:
Hi, I'm developing a web service with should be used by an .NET CF2 client and an .NET 2.0 Windows client. I've tried to put all the connection logic into one class, which could be used in common from the mobile and the windows client. But this didn't work: The new .NET framework supports asynchronous web services with the "MethodnameCompleted+=new MethodNameEventHandler pattern, but the "old style" .NET 1 pattern used in the compact...
5
2298
by: Ryan Liu | last post by:
Hi, I read Microsoft SDK, ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/cpovrasynchronousprogramming overview.htm there are 4 ways to call EndInvoke: The code in this topic demonstrates four common ways to use BeginInvoke and EndInvoke to make asynchronous calls. After calling BeginInvoke you can:
3
2089
by: =?Utf-8?B?bWs=?= | last post by:
Hi everyone, I need to refactor some of our processes using the asynchronous programming model. I have defined a couple of arrays of delegates to pipline the asynchronous invocations through different stages of execution. However I was wondering if there was any information available regarding the limitations of the asynchronous model, in particular maximum numbers of asynchronous elements, and limitations of multiple requests potentially...
3
1525
by: senfo | last post by:
I recently read an MSDN article by Jeff Prosise titled, Scalable Apps with Asynchronous Programming in ASP.NET (http://msdn.microsoft.com/msdnmag/issues/07/03/WickedCode/). In the article, Jeff discusses scaling problems that some users experience with ASP.NET applications. The problem, Jeff writes, isn't that ASP.NET isn't capable of scaling; but has more to do with ASP.NET applications that use threads inefficiently. In the article,...
2
1453
by: =?Utf-8?B?U3JpcmFtIE1hbGxhanlvc3VsYQ==?= | last post by:
Hi, I was going through the article (http://msdn.microsoft.com/msdnmag/issues/05/10/WickedCode/) regarding async programming in ASP.Net 2.0 but did not find much of an use with it. Actually my requirement is that once a user logs in I need to call a web service which will perform some activity but I am not bothered with the result of the web service. I have a process.aspx which is called after login and after implementing the code...
0
8357
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8581
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.