Rob Garrett - Blogs

Welcome to Rob Garrett - Blogs Sign in | Join | Help
in Search
Google

Software/Technology Discussion

Software and Technology Tid-bits

Thread Marshalling

Thought: Wow, haven't posted much in the software blog recently - been so busy writing software that I haven't had time to talk about it.

This week I found out about thread marshalling. Simply put, it involves marshalling of execution through win-form controls from worker threads. Justin Rogers writes and excellent article about it here, so I'll not go too far into the gory details (or not so gory as it appears).

I wrote a cool thread manager component that maintains a queue of jobs. The thread manager permits the execution of  a finite number of worker threads, one per job up to a maximum limit. Once the maximum number of threads are allocated, and busy, the thread manager will queue up subsequent jobs until a worker thread becomes free to process the job.

My thread manager component works well, so I built a print manager on top of it. The print manager manages print jobs (as the name suggests) and monitors the print spooler for the health of print jobs. When a print job starts, completes, or is aborted the print manager will fire an event.

I was talking with a colleague about this the other day and he mentioned that I need to be careful when calling win-form controls from my print manager event handlers, since they run on a different thread to the win-form application. So I looked into this problem a little further...

Windows maintains a message pump for each windowed application and pushes messages onto the pump when an event occurs. This ensures that multiple window events, such as a mouse click or a keyboard press, do not conflict and the event handlers execute sequentially. This is all fine and dandy until a worker thread fires an event, which, when trapped by an event handler, requires some feedback sent to the user. Fortunately the .NET framework provides a nice little solution.

Each event handler that responds to events fired by a worker thread should check to see if the calling thread is the same as that of the host win-form application (UI thread). The Control class has a method called InvokeRequired, which will return true if the the current thread is not the UI thread. Once it's established that the threads are different, a call to Control.Invoke will ensure that changes to a win-form control are made on the UI thread.
Share this post: Email it! | bookmark it! | digg it! | reddit!
Published Thursday, February 03, 2005 6:28 PM by Rob Garrett

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

mike said:

Do you have sourcecode of the thread manager component available?
May 9, 2005 4:04 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

Blurb


Head Shot
Rob Garrett is a British Expat living in Maryland USA. Rob is a trained software engineer and experienced in Windows .NET development.

Rob enjoys listening to Rock music, posting to blogs, driving in the country with the sunroof open, beer (not in conjunction with country driving) and spending time with his family.

This Blog

Syndication

Powered by Community Server, by Telligent Systems