Click here to Skip to main content
Licence CPOL
First Posted 29 Aug 2005
Views 29,690
Bookmarked 28 times

A Message Loop object

By | 29 Aug 2005 | Article
Asynchronous sequential execution of methods using a message loop.

Sample Image - messageloop.jpg

Introduction

Let's look at the following scenario: you have an object that should do something asynchronously. However, if the same action is required many times, you want to keep the order of the original calls. In other words, you want the object to keep an internal buffer of calls, and process them sequentially on a separate thread (all in the same thread that is different from the calling thread). We all know this simply as: a message loop.

This could be regarded as a specific case for a 1-thread threadpool. However, since it is a special case, you can make it much simpler than a fully functional threadpool. BTW, if you want a really well-implemented, feature-rich threadpool, you should definitely have a look at Ami Bar's Smart Thread Pool.

How does it work?

All calls are saved in an internal queue. The queue is emptied by one single thread in sequential order (FIFO), and each time an item is extracted from the queue, its corresponding action is executed. The objects in the queue hold a delegate to the method to be invoked and a state object, similar to ThreadPools.

To avoid overfilling the buffer, you can predefine a maximum buffer size. When the buffer reaches that size, any additional message is being dropped, until some free space is available on the buffer (the default size is int.Max).

Where did I find it useful?

I have several places where I have to read messages from a MSMQ queue and do something upon receiving a message. Sometimes, the order of the messages is very important, and I should not start running with one message before the previous ones are completed. Another good example is with various logging mechanisms. With logging, the order of the messages is extremely important. However, you want the thread to return to the caller as soon as possible, in order to diminish the overhead of using logging. So using a message loop, I can put the message to the queue real fast, and log it in the background, hence providing asynchronous sequential logging.

What's in the demo?

The demo is really simple, it just shows you that the messages are indeed processed in the order in which they were added to the queue. As a contrast, you can unmark the line that sends the messages to the thread pool and see the difference.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ilan Assayag

Software Developer (Senior)

Israel Israel

Member

I am an MSc. student at the Interdisciplinary Center Herzlia, Israel (www.idc.ac.il)
Also, I work as private consultant in the fields of OOP/OOD, C++, C#, SQL Server and solving complex problems with AI and Machine Learning methods.
See my Blog at: http://ilanas.blogspot.com/

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalisrael is rubish... Pinmembermohammad-mirshahi21:29 31 Jan '09  
GeneralRe: israel is rubish... PinmemberJim Crafton10:45 3 Mar '09  
GeneralRe: israel is rubish... PinmemberDewey13:05 17 Nov '09  
QuestionHave you got Unit test for this project? Pinmemberleeanthonyw684:54 11 Jul '08  
AnswerRe: Have you got Unit test for this project? PinmemberIlan Assayag18:08 11 Jul '08  
GeneralUnit Tests Pinmemberleeanthonyw684:18 11 Jul '08  
QuestionWhy do we need Interlockeds in AddToBuffer? Pinmemberfengtian3:12 13 Nov '06  
AnswerRe: Why do we need Interlockeds in AddToBuffer? PinmemberIlan Assayag5:48 13 Nov '06  
GeneralRe: Why do we need Interlockeds in AddToBuffer? Pinmemberfengtian7:58 13 Nov '06  
Hi, Ilan,
 
Thnaks for your quick reply Smile | :) Yes, you're right. I was confused by the following code,
if (bufferQueue.Count > queueLimit)
if (Interlocked.CompareExchange(ref dropMessages, 1, 0) == 0)
Now, I realize that synchronization lock has been released afte the first "if statement", and the senario you give here makes "Interlocked" absolutely necessary.
 
Fengtian

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 29 Aug 2005
Article Copyright 2005 by Ilan Assayag
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid