Reputation: 57227
I've got a long-running task that is executed in a thread.
The code for this all takes place in a separate class than the WinMain()
entry point, say WorkerClass
.
I'd like to update a progress bar every so often with the progress of the task.
After several hours of research, trial, and error, I've come across several methods:
A) Custom PostMessage
to main window proc from inside the thread (problem: frowned upon by several sources: thread should not touch main UI)
B) Custom PostMessage
to main window proc from some WorkerClass
function (problem: running into scope issues between free vs class functions, since thread function has to be free of class)
C) Worker thread updates int
pointer, then a UI-thread timer updates UI periodically (problem: worth spending time on, or is this "doing it wrong")
A lot of the discussion dives into MFC or C# which I'm not interested in.
I'm to the point where I've run into roadblocks with all three methods. It will be several hours to do a good job on figuring one out - I don't want to put in effort learning it wrong.
Which one should I pursue?
Upvotes: 2
Views: 4190
Reputation: 688
FWIW: The code base I currently work on posts a message to the main thread.
Another method, and the one I prefer, is the Observer Pattern. The main thread would "subscribe" to the worker and whenever the worker updates itself, it notifies its observers (any object that subscribed to it). http://en.wikipedia.org/wiki/Observer_pattern
Upvotes: 2