MFC dialog box + threading

Started by
4 comments, last by Endurion 15 years, 9 months ago
Greetings, I've got an MFC application setup using the Visual Studio 2008 wizard. Things are going pretty well except for one thing. When the user clicks one of the buttons, I spawn a thread that goes off and does some work for X amount of time or until the user cancels the operation. This thread takes two of the CEdit box contents as input, one of them being a number. When the thread is done working, that number needs to be updated. There's where my problem occurs. How do I update the number when the thread is done? Currently, I pass the 'this' pointer as a parameter to the worker thread so it has access to the dialog box. However, calling UpdateData() via the passed 'this' pointer always causes a crash. So my question is, how do I call UpdateData(false) (in order to update the number in the CEdit box when the worker thread is done) if I can't call it from the worker thread itself right before it exits? Thanks!
Advertisement
I'd keep your UI single threaded, and have the worker thread take a pointer to a structure (Or just the number) as a parameter, which it can update. When the thread exits, the main thread should notice this (The worker thread could set an event handle to signal this), and update the UI with the new value.

That way the worker thread has no direct effect on the UI.
Quote:Original post by Evil Steve
I'd keep your UI single threaded, and have the worker thread take a pointer to a structure (Or just the number) as a parameter, which it can update. When the thread exits, the main thread should notice this (The worker thread could set an event handle to signal this), and update the UI with the new value.

That way the worker thread has no direct effect on the UI.


How would the event handle work? If I try calling UpdateData() in any function called by the thread, the application crashes. I currently don't have any sort of Idle process that can look for the thread to exit before executing UpdateData(); I'm going to need a few more details to figure this one out. :(
Forget UpdateData. Why don't you set the affected controls directly? Window messages are per design threadsafe.

UpdateData always acts on ALL bound controls. Which is bound to create problems if a change event triggers a change in a different control.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Quote:Original post by Endurion
Forget UpdateData. Why don't you set the affected controls directly? Window messages are per design threadsafe.

UpdateData always acts on ALL bound controls. Which is bound to create problems if a change event triggers a change in a different control.


If you suggest not using UpdateData(), are you then saying that I should never add a Variable that is a Value type and always make them Control types? If I add a Variable of Value type, how can I get the currently displayed value without calling UpdateData()?
All the controls have at least a SetWindowText function. Others may vary (combo or listbox for example have SetCurSel).

It is a bit of extra work. You need to have a variable for the content and you have to retrieve and display it yourself.
But for me UpdateData always led into trouble with more complicated event chaining. It really depends though on what events trigger what behaviour. Your mileage may vary though.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement