Jump to content
  • Advertisement
Sign in to follow this  
Mantear

MFC dialog box + threading

This topic is 3804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :(

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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()?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!