Multi-tasking and threads
-
I have a dialog based app which has a progress bar and other controls. I want to process some stuff in the background moving the progress bar, but not blocking the main process thread. I tried using threads but the child threads cannot move the progresss bar even if i pass the progress bar argument to the child thread. Does anybody have any ideas?? Another idea could be create a new process and let it run and use intrprocess communication but still the parent process is blocked I think i am just complicating things because many applications have this functionality Any ideas would be greatly appreciated. Kelvin Chikomo
theprinc wrote:
I tried using threads but the child threads cannot move the progresss bar...
Nor should they. The secondary thread(s) should be posting a message back to the primary thread (which owns the progress control) to update the UI.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
theprinc wrote:
I tried using threads but the child threads cannot move the progresss bar...
Nor should they. The secondary thread(s) should be posting a message back to the primary thread (which owns the progress control) to update the UI.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
How do you post messages back to the primary thread without blocking the primary thread. Any sample code would help Kelvi Chikomo
When you start your second thread, you give it the handle to the window to which it will post messages. Then, to post messages, simply use PostMessage which doesn't block the thread.
-
How do you post messages back to the primary thread without blocking the primary thread. Any sample code would help Kelvi Chikomo
theprinc wrote:
How do you post messages...
By using
PostMessage()
.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
theprinc wrote:
How do you post messages...
By using
PostMessage()
.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
Say I use the PostThreadMessage() method the primary thread would have to have some sort of a loop to keep reading the messages therefore keeping the primary process busy. The secondary thread would be sending lots of messages Kelvin Chikomo
The primary thread, the one that owns the GUI, has a message queue, and a message pump to process messages in that queue. See here for more.
theprinc wrote:
The secondary thread would be sending lots of messages
For just the updating of a progress bar? Even so, it's doubtful you'll exceed the maximum size of the message queue. You might look at using an I/O Completion Port to queue messages to the main GUI thread. See here for an example.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
The primary thread, the one that owns the GUI, has a message queue, and a message pump to process messages in that queue. See here for more.
theprinc wrote:
The secondary thread would be sending lots of messages
For just the updating of a progress bar? Even so, it's doubtful you'll exceed the maximum size of the message queue. You might look at using an I/O Completion Port to queue messages to the main GUI thread. See here for an example.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
Thanks dude. I am going to try it using peekMessage() in an MFC environment hope it works Kelvin Chikomo
theprinc wrote:
I am going to try it using peekMessage() in an MFC environment hope it works
Why? An application's primary thread already has a message pump. No need to reinvent the wheel.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
theprinc wrote:
I am going to try it using peekMessage() in an MFC environment hope it works
Why? An application's primary thread already has a message pump. No need to reinvent the wheel.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
For most applications, there is no need to interface with the message pump, or even the message queue. It's all handled nicely for you. See that worker-thread link I referenced earlier.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli