Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello
I'm implementing a MFC S/W and it has two buttons to create a thread to log data and cancel it.
When a button "start" is clicked, it supposed to create a thread by
C++
hThread_log = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)LogData_thr, this, NULL, &thrId_log);

The function LogData_thr has while(1) in it.
And when a button "stop" is clicked, it supposed to stop the thread.
I tried TerminateThread() with CloseHandle() and some flag variable which breaks the infinite loop in the function LogData_thr.
However, every trial caused a problem when I put "start" button after stopping the thread (after the function is returned).
Something unwanted happens inside the function LogData_thr.
How can I manipulate the thread?
I always appreciate your help you have given to me.
Posted
Updated 11-Jul-15 4:31am
v5
Comments
Frankie-C 11-Jul-15 10:34am    
What means "Something unwanted happens inside the function LogData_thr" ?
What happens when you call it the second time?
Can you show us part of the LogData_thr code?

1 solution

There is the function TerminateThread for such issues. I like more setting a global variable which is checked for exiting the thread by its own, so it can do some cleanup.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900