I want to use a thread to run a conversion process. This needs access the the main windows variables. Do I use AfxGetMainWnd() to do this?
What is the best way for an application to tell the thread to terminate?
Printable View
I want to use a thread to run a conversion process. This needs access the the main windows variables. Do I use AfxGetMainWnd() to do this?
What is the best way for an application to tell the thread to terminate?
if your thread doesn't need user input/output, one way to do this may be:
1) define a structure containing
* the variables you want to share between mainframe and thread
* an handle to two CEvent, you get it with CreateEvent, I will call them suicide and isKilled (Don't forget to reset them);
* an handle to a mutex for synchronization
2) Call AfxBeginThread to launch the worker-thread
In your tread, have an infinite loop, in which you may check if the event is set; This "suicide-event" is set by the MainFrame; if it is, do what you have to do and set the event isKilled; So the mainframe knows that the thread is dead.
By the shared structure, the thread has access to the variables of frame
The mutex is used to protect your datas accessed by both frame and thread. (Use WaitForSingleObject(...))
HTH.
K.
Ash to ash and clay to clay, if the enemy doesn't get you, your own folk may.