473,569 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading Best Practice Question

RWF
I have read that when using asynchronous IO it is best to keep all your
operations asynchronous. But why if there is nothing left do in the
AsyncCallback method other than to call an IO operation? Example:

//DeliveryHelper is just a class containg an TcpClient and other state
traced variables.
//HelpState.CommS tream is a property that returns TcpClient.GetSt ream()

public void some method()
{
Dns.BeginGetHos tAddresses(t[0].Exchange, new
AsyncCallback(E ndingMXResolve) , HelperState);
}

public void EndingMXResolve (IAsyncResult result)
{
IPAddress[] listedIPs=Dns.E ndGetHostAddres ses(result);
DeliveryHelper HelperState = result.AsyncSta te;

HelperState.Mes sageClient.Begi nConnect(listed IPs, 25, new
AsyncCallback(E ndingConnect), HelperState);

//nothing left to do...
}

public void EndingConnect(I AsyncReslut result)
{
//end connect;
//begin async read;

}

______________V s______________ ________
void some method()
{
Dns.BeginGetHos tAddresses(t[0].Exchange, new
AsyncCallback(E ndingMXResolve) , HelperState);
}

public void EndingMXResolve (IAsyncResult result)
{
IPAddress[] listedIPs=Dns.E ndGetHostAddres ses(result);
DeliveryHelper HelperState = result.AsyncSta te;

HelperState.Mes sageClient.Conn ect(listedIPs,2 5);

//once connected do a read;
}
Feb 15 '06 #1
3 4926
Generally, if you start Async operation you schedule a task on thread pool,
right?
Lets consider following scenario: there is great number of request thread
pool may quickly run out of free threads.
Then if you will introduce sync operation like
HelperState.Mes sageClient.Conn ect(listedIPs,2 5); Then threadpool thread will be busy completing sync request. In this
situation possibility to run out of threads is high.

OTOH if you will schedule operations via BeginXXX functions then possibility
to starve threads is lower, because threadpool thread will schedule
operation and return to pool immediately.
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

"RWF" <RW*@discussion s.microsoft.com > wrote in message
news:EF******** *************** ***********@mic rosoft.com...I have read that when using asynchronous IO it is best to keep all your
operations asynchronous. But why if there is nothing left do in the
AsyncCallback method other than to call an IO operation? Example:

//DeliveryHelper is just a class containg an TcpClient and other state
traced variables.
//HelpState.CommS tream is a property that returns TcpClient.GetSt ream()

public void some method()
{
Dns.BeginGetHos tAddresses(t[0].Exchange, new
AsyncCallback(E ndingMXResolve) , HelperState);
}

public void EndingMXResolve (IAsyncResult result)
{
IPAddress[] listedIPs=Dns.E ndGetHostAddres ses(result);
DeliveryHelper HelperState = result.AsyncSta te;

HelperState.Mes sageClient.Begi nConnect(listed IPs, 25, new
AsyncCallback(E ndingConnect), HelperState);

//nothing left to do...
}

public void EndingConnect(I AsyncReslut result)
{
//end connect;
//begin async read;

}

______________V s______________ ________
void some method()
{
Dns.BeginGetHos tAddresses(t[0].Exchange, new
AsyncCallback(E ndingMXResolve) , HelperState);
}

public void EndingMXResolve (IAsyncResult result)
{
IPAddress[] listedIPs=Dns.E ndGetHostAddres ses(result);
DeliveryHelper HelperState = result.AsyncSta te;

HelperState.Mes sageClient.Conn ect(listedIPs,2 5);

//once connected do a read;
}

Feb 15 '06 #2
RWF
Yep. I walked through the logic after I posted and realized the issue with
the threadpool. For some reason I was assuming a completely new thread was
being created outside the pool, which is of course not the case.

"Vadym Stetsyak" wrote:
Generally, if you start Async operation you schedule a task on thread pool,
right?
Lets consider following scenario: there is great number of request thread
pool may quickly run out of free threads.
Then if you will introduce sync operation like
HelperState.Mes sageClient.Conn ect(listedIPs,2 5);

Then threadpool thread will be busy completing sync request. In this
situation possibility to run out of threads is high.

OTOH if you will schedule operations via BeginXXX functions then possibility
to starve threads is lower, because threadpool thread will schedule
operation and return to pool immediately.
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

"RWF" <RW*@discussion s.microsoft.com > wrote in message
news:EF******** *************** ***********@mic rosoft.com...
I have read that when using asynchronous IO it is best to keep all your
operations asynchronous. But why if there is nothing left do in the
AsyncCallback method other than to call an IO operation? Example:

//DeliveryHelper is just a class containg an TcpClient and other state
traced variables.
//HelpState.CommS tream is a property that returns TcpClient.GetSt ream()

public void some method()
{
Dns.BeginGetHos tAddresses(t[0].Exchange, new
AsyncCallback(E ndingMXResolve) , HelperState);
}

public void EndingMXResolve (IAsyncResult result)
{
IPAddress[] listedIPs=Dns.E ndGetHostAddres ses(result);
DeliveryHelper HelperState = result.AsyncSta te;

HelperState.Mes sageClient.Begi nConnect(listed IPs, 25, new
AsyncCallback(E ndingConnect), HelperState);

//nothing left to do...
}

public void EndingConnect(I AsyncReslut result)
{
//end connect;
//begin async read;

}

______________V s______________ ________
void some method()
{
Dns.BeginGetHos tAddresses(t[0].Exchange, new
AsyncCallback(E ndingMXResolve) , HelperState);
}

public void EndingMXResolve (IAsyncResult result)
{
IPAddress[] listedIPs=Dns.E ndGetHostAddres ses(result);
DeliveryHelper HelperState = result.AsyncSta te;

HelperState.Mes sageClient.Conn ect(listedIPs,2 5);

//once connected do a read;
}


Feb 16 '06 #3
Hi RWF,
I have read that when using asynchronous IO it is best to keep all your
operations asynchronous.
Yep. Async usually work on one or a set number of threads. The whole
exercise is to get unblockable jobs multiplexed onto a limited set of
threads efficiently--eliminating the cost of spawning new threads. [Free]
Thread starvation of the limited set of threads in the thread pool is the
result of having any long-running jobs--such as jobs with blocking calls.
Some thread pools accomodate this by starting more threads, but once you
spawn enough threads you lose the benefits of a thread pool because your
CPUs are busy baby-sitting the large number of user threads.

--
Li-fan Chen
Software analyst/developer, Entrepreneur
Markham, Ontario, Canada
"RWF" <RW*@discussion s.microsoft.com > wrote in message
news:6B******** *************** ***********@mic rosoft.com... Yep. I walked through the logic after I posted and realized the issue
with
the threadpool. For some reason I was assuming a completely new thread
was
being created outside the pool, which is of course not the case.

"Vadym Stetsyak" wrote:
Generally, if you start Async operation you schedule a task on thread
pool,
right?
Lets consider following scenario: there is great number of request thread
pool may quickly run out of free threads.
Then if you will introduce sync operation like
> HelperState.Mes sageClient.Conn ect(listedIPs,2 5);

Then threadpool thread will be busy completing sync request. In this
situation possibility to run out of threads is high.

OTOH if you will schedule operations via BeginXXX functions then
possibility
to starve threads is lower, because threadpool thread will schedule
operation and return to pool immediately.
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

"RWF" <RW*@discussion s.microsoft.com > wrote in message
news:EF******** *************** ***********@mic rosoft.com...
>I have read that when using asynchronous IO it is best to keep all your
> operations asynchronous. But why if there is nothing left do in the
> AsyncCallback method other than to call an IO operation? Example:
>
> //DeliveryHelper is just a class containg an TcpClient and other state
> traced variables.
> //HelpState.CommS tream is a property that returns TcpClient.GetSt ream()
>
> public void some method()
> {
> Dns.BeginGetHos tAddresses(t[0].Exchange, new
> AsyncCallback(E ndingMXResolve) , HelperState);
> }
>
> public void EndingMXResolve (IAsyncResult result)
> {
> IPAddress[] listedIPs=Dns.E ndGetHostAddres ses(result);
> DeliveryHelper HelperState = result.AsyncSta te;
>
> HelperState.Mes sageClient.Begi nConnect(listed IPs, 25, new
> AsyncCallback(E ndingConnect), HelperState);
>
> //nothing left to do...
> }
>
> public void EndingConnect(I AsyncReslut result)
> {
> //end connect;
> //begin async read;
>
> }
>
> ______________V s______________ ________
>
>
> void some method()
> {
> Dns.BeginGetHos tAddresses(t[0].Exchange, new
> AsyncCallback(E ndingMXResolve) , HelperState);
> }
>
> public void EndingMXResolve (IAsyncResult result)
> {
> IPAddress[] listedIPs=Dns.E ndGetHostAddres ses(result);
> DeliveryHelper HelperState = result.AsyncSta te;
>
> HelperState.Mes sageClient.Conn ect(listedIPs,2 5);
>
> //once connected do a read;
> }


Feb 16 '06 #4

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

Similar topics

136
9251
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to...
6
3255
by: MPH Computers | last post by:
Hi I am looking for some help on Threading and Critical Sections I have a main thread that controls an event the event handler creates a new thread for carrying out the work because the work may not be completed before the event is triggered again I am trying to add critical sections round the producer & consumer parts of
0
5169
by: Kelsang Wangchuk | last post by:
Hi Just a quick question... When would you use System.Timers.Timer, and when System.Threading.Timer? What are the principal differences between them? There is a lot of discussion about System.Timers.Timer and its use in running background server activities, but not much about System.Threading.Timer other than in the reference, which...
13
1800
by: John | last post by:
I've got some reasonably complex business logic in my C# code, in a class called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not dependent on SQL calls or anything like that. I know of the problems with the worker process doing two things at once from the main thread pool (The old "Sit in a tight loop for 20 seconds and...
7
318
by: Anthony Nystrom | last post by:
What is the correct way to stop a thread? abort? sleep? Will it start up again... Just curious... If the thread is enabling a form, if the form is disposed is the thread as well? Thanks, Anthony Nystrom
4
1294
by: Bob | last post by:
- For cleanup, is it sufficient to set a Thread to Nothing after it's done? - It is OK to pass objects out of the thread? (dumb question maybe but I want to be sure) - What's the best way to process messages coming out of a thread? I want to queue them up, but MessageQueue doesn't look like what I need. Should I just make my own queue...
10
2972
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? public class MyClass private _variableName as integer public property VariableName as integer
5
10880
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I have a form that has a button. ( this form is a child form of a parent form ( main form ). Anway...in this child form I have a button, and if clicked...
126
6649
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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. ...
1
7678
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...
0
7982
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...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3656
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...
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
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...

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.