I have this code:
// ------- My dialog header file ----------
class CMexifierDlg : public CDialogEx {
public:
unsigned int static CMexifierDlg::doit(LPVOID pParam); // this is the thread
CMexifierDlg* pMexifierDlg=this;
int x, // used to initialize the dialog AND by thread
y, // set by dialog, used to pass info to thread
z // used by MULTIPLE thread functions, but NOT by dialog
};
// ------- My CPP file -----
void CMexifierDlg::OnClickedCheck1() {
CMexifierDlg* myparm; myparm = pMexifierDlg;
AfxBeginThread(doit, (LPVOID)myparm);
// thread terminates itself when job is done
}
// ----- my thread function
unsigned int CMexifierDlg::doit(LPVOID pParam) {
// this is where the actual work happens
CMexifierDlg* D_ptr=(CMexifierDlg*)pParam;
int y = x; // doesn’t compile
int y = D_ptr->x; // fails at runtime
// now we make many calls (as part of thread)
// to other functions that need D_ptr
return 0; // thread completed successfully
}
The header file has MANY variables, some of which are needed ONLY by my main (and only) dialog, some of which are shared by the dialog and the ONE thread I create from the dialog when a button is pressed and some that are used ONLY by many functions called within the thread. Once the thread starts, it runs to completion before ANY other actions will be allowed in the dialog, except exiting the whole program. However, once the thread is done, it can be restarted with NEW data from the dialog. Can someone SHOW me what I have wrong that will make this work? I am getting lost with understanding HOW to pass the correct thing to my thread. One thing I tried is to MOVE my thread-only var definitions to the .CPP file as global data and that SEEMS to be OK (but is it a proper thing to do)?