help in MFC threads UI - c++

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.

Related

Calling methods on an MFC UI class in a separate thread

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.

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

Unable to understand Undo Redo Framework in Qt

I am learning to use Qt for my application development & I am pretty successfull in developing my Application. Now I want to implement Undo Redo Functionality for my Application. The doc for this topic has little information. I have even tried understanding from the 2 examples in the SDK. But I am having a tough time understanding how it works.
Can somebody please take the trouble of explaining me how to implement it?
There are various state's in my application for which I want to provide this functionality.
So can the explaination be from the general point of view?
If there are already articles on the internet explaining the same then please notify me about them. That would be very helpful.
Thank You.
There are 2 core classes: QUndoCommand and QUndoStack;
QUndoCommand is the base class of your command class. You have to implement undo() and redo() yourself.
QUndoStack is basically a container of QUndoCommand objects, with extra methods like creating QAction, query undo/redo text of current QUndoCommand(Simple functionalities which you can implemented yourself easily)
What you need to do is:
Implement your commands. You need to decide how to implement redo/undo yourself according to your need. class AppendText is a good example: http://qt-project.org/doc/qt-5.0/qtwidgets/qundocommand.html
Keep a QUndoStack instance for each document(Or one instance if there is only one document in the application).
Say you have an "AppendText" command class, and an "Append" button in the UI. If "Append" button is clicked, you need to create an AppendText command instance, and call QUndoStack::push(appendCmd). QUndoStack::push() will call AppendText::redo() first, and then put it into the container for undo.
That's it.

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 ?

Create a background process with system tray icon

I'm trying to make a Windows app that checks some things in the background, and inform the user via a systray icon.
The app is made with Not managed C++ and there is no option to switch to .net or Java.
If the user wants to stop the app, he will use the tray icon.
The app can't be a Service because of the systray side and because it must run without installing anything on the user computer ( it's a single .exe )
Using the typical Win32 program structure ( RegisterClass, WndProc and so on ) i dont know how can i place some code to run apart the window message loop.
Maybe i have to use CreateProcess() or CreateThread()? Is It the correct way to handle the Multithreading environment?
If i have to use CreateProcess()/CreateThread(), how can i comunicate between the two threads?
Thanks ;)
As for the system tray icon, you'll need Shell_NotifyIcon.
See http://msdn.microsoft.com/en-us/library/bb762159.aspx
I doubt you want to create new processes to do this, you want to create a thread in your application. The API to do this is CreateThread. But if you are using C++, you should really be investigating the use of frameworks and class libraries to do this, not writing what will effectively be C code from scratch.
All threads belonging to an application share the global variables of the application, which can thus be used for communication. You will need to protect such multi-threaded access with something like a critical section.