The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Google Ads

This Blog

Syndication

Search

Tags

News





  • View Maurice De Beijer's profile on LinkedIn

Community

Email Notifications

Explore

Archives

How not to use Async function calls
The following is a piece of code I recently ran into:
 
IAsyncResult asyncResult = m_Queue.BeginPeek();
while (!asyncResult.IsCompleted && m_Running)
    Thread.Sleep(10);
 
if (m_Running)
{
    Message message = m_Queue.EndPeek(asyncResult);
    ProcessMessage(message);
}
 
Now this is already running in a background thread, not that it makes a lot of difference though. But calling an async  begin function, then sleeping until it is finished and retrieving the result makes no sense whosoever. You might as well just call the regular Peek() and be done with it.
 
So the rational behind writing it this way? Well Peek raises and exception if there is no message in the queue and exceptions are expensive so the developer wanted to avoid them at all cost, even in a background thread. Not exactly my idea of clean and readable code. The guilty know who they are :-(
 
 
Maurice de Beijer
Published Tue, Jun 20 2006 23:04 by Maurice
Filed under:

Comments

# cheap propecia buy online@ Saturday, May 30, 2009 3:55 PM

thanks !! very helpful post!