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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MFC Items and Threads

Status
Not open for further replies.

sweep123

Technical User
May 1, 2003
185
0
0
GB
I have an MFC application which create a thread to receive data from another application.

Now the thread is started and passed the pointer (this) of the main MFC application in order for it to access the methods/properties of the main MFC application that require updating.

This causes an Assertion fault, and it was suggested that PostThreadMessage be used to communicate from the thread to the main application.

My question is - what is the scheme to use for this activity, where the GUI needs to reflect data received, but when the
receive processing is done in a different thread.

How would this be done?

I have tended to pass into the thread the neccessary data/pointer the thread required, but this time I have problems.
If I use a smple timer in place of the thread all is well, but have to run slow to ensure that I do not loose data.
 
The answer is depending on how the both application communicate.
Let be the folowing example:
A GUI application is used to do some regular work but this application receives datafiles in a given folder or some characters in a COM2 from a device. When a file arrives in that folder or some char arrive in the COM2: the application must present the data to the user.
One way to implement it is to have a thread to watch for updates in the given folder or to watch the COM2:.
The watching process uses events and not timer.
For example , to watch a COM port the thread function should include:
CreateEvent()
WaitCommEvent() // This function waits until the event occurs
ReadFile() // When the event occurs then read the chars from the port
SendMessage() // Send a custom message to the application (owner of this thread) to take actions
For other cases you should use WaitForSingleObject () or WaitForMultipleObjects() instead of timers.

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top