0
votes

I am fixing a GUI application programmed with MFC. To run a communication stack a new thread is created. On some events I want to stop this but I know that only in the worker thread and I cannot get the correct behaviour from stopping the worker thread from itself. So I need to send a message to the main thread and tell it to stop the worker thread so that the main thread can then take the correct action.

I am unsure how to do this. I have looked at the SendMessage function in CWnd but it does not take threadid as an argument and I dont get what I need to include to use http://msdn.microsoft.com/en-us/library/windows/desktop/ms644944(v=vs.85).aspx.

1
'I cannot get the correct behaviour from stopping the worker thread from itself' - why? I suspect that data that should belong in the thread is not - you got comms components plonked onto the form, by any chance, or is it the other way round - the GUI directly accesses data in the thread class?Martin James
Well if there is a reset command coming into the comm stack the stack should stop and start itself. problem is if I call the Stop function then the thread is terinated and then it obv cannot call the start function since it no longer exists.user1190832
OK, don't stop the thread. Put a while(true){try...catch} round the entire comms init/setup/run code in the thread and, if you detect an error, throw.Martin James
If you use SendMessage/PostMessage with a custom message (WM_APP + x) you can pass anything you like as the WPARAM and LPARAM. The system will not interpret the message for you in any way but will simply pass the parameters you provide to your message handler.IInspectable

1 Answers

0
votes

Actually, the only safe way to do anything to a worker thread is from within the worker thread itself. It should probably be structured as a while(bRun) loop so it runs until the program is shutting down. Whatever you are trying to do (stop and start) should be handled by code in the while loop.