473,899 Members | 4,542 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
48 5465
William Stacey [MVP] <st***********@ mvps.org> wrote:
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.


I believe WaitForMultiple Events() can only wait on 64 event objects at
a time. The OP mentioned a potential peak as high as 100, so if he
used this approach, a modified version would be required. Also I
believe WaitForMultiple Objects() tends to favor the lower numbered
objects which could present problems for certain usage patterns.
Nov 15 '05 #21
William Stacey [MVP] <st***********@ mvps.org> wrote:
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.


I believe WaitForMultiple Events() can only wait on 64 event objects at
a time. The OP mentioned a potential peak as high as 100, so if he
used this approach, a modified version would be required. Also I
believe WaitForMultiple Objects() tends to favor the lower numbered
objects which could present problems for certain usage patterns.
Nov 15 '05 #22
be****@dogs-like-spam.com wrote in
news:40******** *************@n ewsreader.visi. com:
This metric is fairly meaningless because it doesn't indicate how many
Its not meaningless. It wasnt meant to point out that they all could be
active. But nearly all users are under the impression that if you even CREATE
100 threads you will see smoke rolling. This proves this point false.
of those threads are active at any given time. If only a handful of
those threads are active, there won't be much context switching going
on.
As to the second part - you need to read the rest of the message. I have
built servers up to 1,000 thread that run just fine and do not consume tons
or RAM, nor CPU.

In stress testing I've pushed them above 1,500.
If all 493 threads are busy reading/writing data to/from reasonably
active socket connections, you'll be doing lots of context switches per
second, and chewing up a lot more CPU doing those context switches.


Wrong - Please go back and read my complete message. It appear you just read
the first part.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #23
be****@dogs-like-spam.com wrote in
news:40******** *************@n ewsreader.visi. com:
This metric is fairly meaningless because it doesn't indicate how many
Its not meaningless. It wasnt meant to point out that they all could be
active. But nearly all users are under the impression that if you even CREATE
100 threads you will see smoke rolling. This proves this point false.
of those threads are active at any given time. If only a handful of
those threads are active, there won't be much context switching going
on.
As to the second part - you need to read the rest of the message. I have
built servers up to 1,000 thread that run just fine and do not consume tons
or RAM, nor CPU.

In stress testing I've pushed them above 1,500.
If all 493 threads are busy reading/writing data to/from reasonably
active socket connections, you'll be doing lots of context switches per
second, and chewing up a lot more CPU doing those context switches.


Wrong - Please go back and read my complete message. It appear you just read
the first part.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #24
Thanks. Good to know. If you use EventSelect() and use the same event
object for each socket, is there a way to tell what socket caused the event
to fire? TIA

--
William Stacey, MVP

<be****@dogs-like-spam.com> wrote in message
news:40******** *************@n ewsreader.visi. com...
William Stacey [MVP] <st***********@ mvps.org> wrote:
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.


I believe WaitForMultiple Events() can only wait on 64 event objects at
a time. The OP mentioned a potential peak as high as 100, so if he
used this approach, a modified version would be required. Also I
believe WaitForMultiple Objects() tends to favor the lower numbered
objects which could present problems for certain usage patterns.

Nov 15 '05 #25
Thanks. Good to know. If you use EventSelect() and use the same event
object for each socket, is there a way to tell what socket caused the event
to fire? TIA

--
William Stacey, MVP

<be****@dogs-like-spam.com> wrote in message
news:40******** *************@n ewsreader.visi. com...
William Stacey [MVP] <st***********@ mvps.org> wrote:
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.


I believe WaitForMultiple Events() can only wait on 64 event objects at
a time. The OP mentioned a potential peak as high as 100, so if he
used this approach, a modified version would be required. Also I
believe WaitForMultiple Objects() tends to favor the lower numbered
objects which could present problems for certain usage patterns.

Nov 15 '05 #26
"William Stacey [MVP]" <st***********@ mvps.org> wrote in news:#qTbXII#DH A.220
@TK2MSFTNGP09.p hx.gbl:
Thanks. Good to know. If you use EventSelect() and use the same event
object for each socket, is there a way to tell what socket caused the event
to fire? TIA


Not unless you log it somewhere..
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #27
"William Stacey [MVP]" <st***********@ mvps.org> wrote in news:#qTbXII#DH A.220
@TK2MSFTNGP09.p hx.gbl:
Thanks. Good to know. If you use EventSelect() and use the same event
object for each socket, is there a way to tell what socket caused the event
to fire? TIA


Not unless you log it somewhere..
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #28
That is kinda the issue however. All you know is that the event was
signaled - you don't know what socket caused the signal (afaict). So I
guess you can wait on the event and enum the active socket list to poll each
one to see if it has some data and then wait on event again in a loop.

--
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:#qTbXII#DH A.220 @TK2MSFTNGP09.p hx.gbl:
Thanks. Good to know. If you use EventSelect() and use the same event
object for each socket, is there a way to tell what socket caused the event to fire? TIA


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

Nov 15 '05 #29
I gotta tell ya Chad, you made me see a new light on this. I went and put
together a virtual socket class that "creates" packets for reading and
created two tests. First test is creating X number of threads and one
socket per thread that reads N number of packets. Second test was creating
same number of sockets in a object that manages the sockets in a collection
and posts an event that a worker thread(s) waits on. Results vary based on
syncing threads with a shared object (i.e. a fake write queue for example)
that you can set to sleep and/or spinWait. I could get ~20,000 threads
running pretty well. Trying to start 30,000 locked up my app each time, but
could just stop it in TaskManager without a crash. I never saw TaskManager
Threads go over ~571, so not sure if that is just showing running threads or
if CLR only releases so many OS threads and does some other management in
the background. Very suprising results. It's not a clear winner in all
cases and not sure of effects of running 1000+ threads for long time in a
server app, but these results are interesting. If anyone wants to play with
this test harness, I can post it to web. It fun to play with different
options to see time effects of injecting sleeps or waits, etc. Cheers!

--
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:u6******** ******@tk2msftn gp13.phx.gbl:
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

The problem with this is there is a limit of 64 per call, so you still need to break it into threads anways. And then you break your code away from
seqeuntial to handling 64 state machines... makes for ugly code.
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.


Indy has support for this using fibers. This part is in SuperCore and

hasnt been ported to .net yet. This part will likely have to be unmanaged until
fibers, etc are accesible as managed objects.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Nov 15 '05 #30

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

Similar topics

6
3491
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
1572
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
8682
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
1561
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
3178
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
2309
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
2105
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
1545
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
1466
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
9997
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
11272
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10971
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
7201
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
5887
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
6081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4720
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4300
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3317
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.