Solved

C++, MFC Event Driven Multithreading Performance and Com+

Posted on 2004-10-05
4
326 Views
Last Modified: 2013-11-25
I have written a Visual C++ (MFC) application that responds to an Active X event that reads data from a serial port. This data is received from an RF base station through the port which connects to multiple RF units. When the event is fired I process the data and return a reponse back through the port to the base station and to an RF unit. Each event is processed in sequence before the next event.  Some events require more processing time than others (such as database lookups). Since there may be data ready on the port while a current event is being processed, I tried a multithreading approach (AfxBeginThread) to speed up processing if one event takes longer than another. This approach is working but when two (or more) threads process at the same time the performance is degraded.

I have considered writing a Com+ application to handle the events simultaneously, but I'm not sure that the multithreading or marshaling will be any better. In a nut shell I need the events to be handled like a mini-server that can process requests in tandem and still provide better performance than the current sequential event handling that I am using. For compatability I need to stick with Visual C++ (6.0) and MFC and time is critical.

I would appreciate any input on the best performance options available. Is the Com+ approach viable? Does each call of the Com+ component within the main application create a separate process?

Carl
0
Comment
Question by:carlmahon
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:raed_hasan
ID: 12235081
When I have used an Active X component for serial communication I found that when executing one event from the active X
(say receive character) and in that event handler I called functions in the active x component (say set rts), any events
in queue would fire and those eventhandlers would run before the called function returned. I don't know if this is
vendor specific (Greenleaf comx) or technology specific (active x). I solved it by relaying the event with a PostMessage
to my self in order to re queue it and then do the actual work in the code for the message handler of the posted
message.

I also use a modeless dialog as a base for my communications classes (and a lot of other classes) instead of a separate
thread (the active x does all neccessary threading anyway). This dialog contains an edit control which displays what is
happening in the serial communication state machine (only if the dialog is visible), like what characters are
received/sent, if checksum is OK, if a command has executed successfully and so on. The dialog also contains controls to
let the user change port settings if the app requires it. The dialog is normaly only visible when troubleshooting the
serial communication. Advantages with the modeless dialog, other than this visual feedback, is that it is a destination
for Post/Send messages and that it is working in the main GUI thread (no threading issues).

Regards
0
 
LVL 4

Author Comment

by:carlmahon
ID: 12243989
Thanks Raed - Very interesting approach using modeless dialog for multithreading, this may work for me but leads me to a brief follow up -

Assume - Event #1 from the active X has fired and I post a message to modeless box #1 and it is running a lengthy process before returning data to the port....
Event #2 is fired from the active X and I post a message to modeless box #2 (instead of a new thread), would both processes (modeless boxes) run in tandem and efficiently from the main GUI thread?
0
 
LVL 2

Accepted Solution

by:
raed_hasan earned 500 total points
ID: 12248012
Hi,

Since the modeless dialogs run in the main GUI thread, any lengthy processes here will block the main GUI thread and You
won't receive any new messages until You return from the lengthy process. In this case you have to use worker threads
instead. The worker threads can be part of the modeless dialog boxes and communicates only with those (they own and
control the worker threads). Your events can post messages to the modeless dialog windows (which in turn activates the
worker threads). This way it will work as you described.

The benefit with a modeless dialog is not so much for multitasking (if processing of messages are quick, it will seem
like multitasking though, but it won't work with long processing times) as it is for giving you a user interface and
feedback for your sub processes when you need it. In my programs they are invisible when all is working as it should and
only brought up when trouble shooting some connected hardware. And, as I said before, it will also give you a mechanism
to post/send messages directly to your tasks (objects based on a modeless dialog) working quietly in the background.

Many times I can break up the work for a lengthy process into smaller chunks. These chunks can be executed in the idle
handler (I have a mechanism to to add and remove tasks from the idle handler). This has the benefit that it runs in the
GUI thread which means that I can directly access GUI components and most of the time I don't need any special
synchronisation as is often necessary when multithreading.

Here are some examples of tasks which are based on modeless dialogs in my programs: Serial communication with RFID
readers, Serial communication with scales, serial communication with GPS receiver, Parallell IO (input and output to
other hardware), File importing (act when a file is dropped into a specific folder), Communication with external
programs, File handling and parsing in the background. Some of these use worker threads others do their work directly in
the main GUI thread either directly in the message handler or by adding a task to the idle handler.

Regards /Raed
0
 
LVL 4

Author Comment

by:carlmahon
ID: 12248728
Thanks - The multitasking aspect was my main focus and you have provided me with substantial insight.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question