Calling methods on an MFC UI class in a separate thread - c++

I have two threads in my MFC application. One of them handles all Wnd stuff and the other some file management. Is it generally a good idea to call some methods on UI classes from other threads? Like for example to update some values the UI fields are showing to user. Or it is a bad practice and I have to send messages to the windows classes? If we try to use locks in UI threads, GUI will freeze frequently which is not really acceptable unless it is for a very trivial job. What is the best practice here? Should we solely use message passing mechanisms when dealing with UI?
There are also some methods on UI which don't involve data or variables. For example telling a window to maximize. Is it bad to call a public method in UI class in this case?
[EDIT]
I forgot to tell about my problem with sending messages. Your parameters a always two pointers and not easily customizable.

Primarily, UI is supposed too be light and responsive. To accomplish that, all the heavy duty works should be handled by a separate thread, wherein it can communicate with the main mfc UI thread using SendMessage().
Commands such as maximizing windows have fairly small overhead and can be accomplished in the UI thread itself. To simulate a long process, the UI can use an animated progress bar, for instance.
Decoupling UI from the program engine also will make your code more modular, reusable and the intent clearer.

Related

Custom message pump for MessageBox (Windows)?

I have a large code base where there are lots of calls to AfxMessageBox(...). What I need to do is while these messageboxes are modal service some external IO (i.e. a network communication).
I've considered writing a custom CDialog and my own message pump - but I'm unsure of how to make the CDialog generic enough that I can reliably create all the messageboxes without having to test them all.
Alternatively I've been looking at hooking into the messageboxes' windproc function through SetWindowsHookEx but this has the limitation that it happens after the messagepump and will not fire if there are no messages for that dialog.
Does anyone have any suggestions on how to acheive this or if either of my two approaches above are worth pursuing. ?

What is a good MFC C++ GUI architecture for multiple similar window arrangements

I am considering a rewrite of an MFC Dialog application I wrote over a decade ago. I originally chose a Dialog application type because in general the user clicks control inputs and the application provides real-time data from hardware via USB and displays the data in edit boxes. The app also writes information to a file. Since the user never does any file editing I didn't see a need for SDI or MDI application type.
I've now learned that the SDI application type along with CFormView provides some interesting advantages such as scrolling, window resizing:
http://forums.codeguru.com/showthread.php?267664-MFC-Dialog-How-to-enhance-a-dialog-based-application-with-Menu-Toolbar
The reason for my rewrite is that my application is un-maintainable and doesn't scale well. Originally I created a CDialog for each type of "screen" that I show but lots of code is common among the various screens. In fact over time I started reusing CDialog classes and with the use of state variables simply hid, renamed, or repositioned many of the controls to make the screen look appropriate in the context of the application.
I tried deriving the CDialog classes from a common base class but I didn't achieve great code reuse this way. I also found that I had common resources that I was constantly passing around to the various windows which just added unnecessary overhead.
As the application evolved it also became difficult to keep track of the state of the application (for processing USB hardware data input, user input, button enabling etc). Using a hierarchical statechart as the basis for my application might clean this up.
Should I consider putting a superset of ALL the necessary controls on ONE CFormView and simply hiding, renaming, and repositioning controls from the very beginning and using a statechart to manage this?
All the examples I see put the code directly in the input control handlers but maybe I should use the handlers to change state and then let the state machine do the decision-making to reduce this spaghetti code.
So my primary questions are:
1. Would SDI would buy me anything for writing and reading files (such as reporting and logging) if the user does not perform editing?
2. Does CFormView make sense?
3. Does it make sense to put all controls on one CFormView instead of multiple classes and Dialog resources?
4. Have you tried using a state machine in MFC such as Boost Statechart or Miro Samek's Quantum Leaps?
http://www.boost.org/doc/libs/1_58_0/libs/statechart/doc/index.html
http://www.state-machine.com/products/index.html

c++ mfc how to continue to use application while action is running [duplicate]

I am making an MFC (document/view) application and I want it to constantly listen in the background for when a device is connected and then automatically copy the files on the device without the user needing to interact or pause/disturb what they are doing.
Is creating a worker thread the same as having a background thread? Would I create it as a function in the document file or as a separate class?
Thanks,
Yes, they behave as a normal background threads, you have a function that gets parameter, and then you can enter your listener loop. I would put this function in separate class, maybe in a form of a singleton class, this way you can easily start/stop your device listener. If you would ever need to send information of progress from this worker thread to GUI, use PostMessage to you GUI windows.
as always MSDN provides tons of documentation:
http://msdn.microsoft.com/en-us/library/975t8ks0%28v=vs.80%29

Singleton or Signal and Slots?

I am trying to reimplement or modify a tab code in a gui application. They are currently using Qt signal and slots system to handle addition and removal of tabs from the tab bar (For example if a tab was being drag from one tab widget to another, the old tab widget will signal the new tab widget that a new tab is coming). I was thinking rather than using that, I could simplify things using a thread safe singleton class. Then when ever a tab is moved, the widget just call on the singleton rather than emitting a signal.
Thanks
Signals and Slots.
Without even starting why the singleton would be bad, the way the data is updated inside Qt would be messed up by the singleton approach.
Don't do that. You are working within an environment and should use the mechanism the framework provides. What about if the UI in the future will have multiple windows and maybe multiple instances?
If possible you should always try to use the way from the framework you are using. This will also help in the future for the maintenance (upgrades, new hires, etc.)
You want to use a singleton, which will accept messages and dispatch them back ? (note: if you use a garden variety object instead of a singleton, you're essentially implementing an Observer pattern).
Then you are reinventing signals and slots, which use a global state internally. Instead of putting work in reinventing some difficult piece of code, why don't you use the already existing signals and slots ?

help in MFC threads UI

hi im using c++ mfc project with UI treads
i have a class name manager and class name calc which should dervied from CWinThread. the manager class need to have the ability to send data to calc and to use some func from it. i have no idea how to make start i dont know how to use CWinThread and how to create the treads. please help.
thanks
I don't understand exactly what you're trying to do, but as far as I remember from the good old mfc days, there was usually a better/simple solution using windows massages than threads to most of the problems. Check if this applys to your situation.
(a window doesn't have to be something you show...)
If you have no idea on UI threads, I wonder how did you conclude that you need UI threads to accomplish something. Here's a nice tutorial on UI threads: UI Threads.
You can send data to an UI thread by calling PostThreadMessage() API.
However, I don't understand why does your design require that the manager class be able to "use" some function that belongs to the UI thread class.