I know that in order to access the UI controls from another thread, I should use PostMessage(). However, PostMessage() is asynchronous, so for example if I try to change the text of an "EDIT" control, I will not be able to delete the text buffer when finished because I do not know when the window procedure will finish processing the message.
So these are two ideas I can think of to access the UI controls from another thread:
- Create a user-defined message, for example, I send to the UI thread something like: "insert these 200 rows (all stored in one string, delimited by '\n' or something) into
ListViewX", and when the UI thread receives this message, it updatesListViewX, and when done, it deletes the string (which is allocated on the heap). - The other approach is to place the code that accesses the UI control inside of a function, and send the function pointer to the UI thread, which will then call it.
Does one of these approaches provide some advantages over the other, and are there other approaches to do this?
SendMessageis another option.SendMessage()could cause a deadlock (which is not always easy to predict). See: flounder.com/workerthreads.htm, the section: Worker threads and the GUI II: Don't touch the GUI.