Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UpdateData problem.

Status
Not open for further replies.

yoxler

Programmer
Apr 14, 2003
2
SE
I've got a nice little button:
void CTest2Dlg::OnButton1()
{
MyThread newThread(&m_SerialText, &m_SerialTextControl, this);

newThread.CreateNewThread();
}

m_SerialText is a variable connected to an Edit Box.
m_SerialTextControl is a control variable to the Edit box.


In the CreateNewThread I do the following
m_SerialText->Insert(m_SerialText->GetLength(), "nice thread");
m_SerialText->Insert(m_SerialText->GetLength(), "\r\n");
m_SerialTextControl->UpdateWindow();

This does not work exacltly the way I want it to work. The edit box text is not updated until the thread is finished.
I've tried to add m_SerialTextControl->UpdateData(FALSE); but then the program craches.

How do you update a text in an edit box from outside the dialog?

 
>How do you update a text in an edit box from outside the dialog?

Typically you have a CString, which you give a value. Then you pass the value in the string to the control in the OnInitDialog() method (normally mapped to the WM_INITDIALOG message with the class wizard)

Either you do this manually (if you're a control freak like me), or you let MFC do it for you.

To do it manually:
{
CMyDialog d;
d.m_TheString = "Foo";
}

CMyDialog::OnInitDialog()
{
.
.
m_MyControl.SetWindowText(m_TheString);
}


To let MFC do it, you simple assign the control to a string in the class wizard and then it will take care of the rest (but it pretty much does the same thing as above).

They trickery with it all is that you can't pass the value to the control before the dialog is created (ie DoModal is called), simply because the control itself wont exist at that point.

Normally you don't have to fiddle with UpdateData. UpdateData should IMHO mainly be regarded as an internal "tool" for MFC to pass data to/from controls and data variables. It is used to pass data to controls as dialog is shown and retrieve data as dialog is closed (OnOK)

If you feel you need to manually call UpdateData, id say you're having a slight problem with your design...

 
>> I've tried to add m_SerialTextControl->UpdateData
>> (FALSE); but then the program craches.

MFC CWnd is not thread safe. This is documented in several Technical Notes on MSDN.

The "short" version is: CWnd to HWND maps are stored in Thread Local Storage and so do not exist in threads other than the thread the CWnd was created in.

To perform this type of threaded notification you must perform all the CWnd functions in the UI thread. You use PostThreadMessage() to send proprietary messages from your worker thread(s) to your UI thread.

-pete

 
Thank you!

I will look into PostThreadMessage(). Do you have an example? I've only worked with vc++ for one week now so I'm a newbie ;o)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top