0

In the MFC Project ,I have main thread focusing on the dialog. another worker thread focusing on receiving data from server ,parsing the data and updating the data into database.

the data from server is quite huge. so I destroy the dialog by clicking the close button while the worker thread is not finished. But the worker thread is not independent. while I delete the object create in the main thread, the worker thread can't continue , that is to say. some error like the access violation problem. (because the worker thread use the member variables in the xxxxdialog class).

the way I create a thread is AfxBeginThread, i use the method waitforsingleobject, still can not work...

I don't want to force to kill the worker thread, otherwise, it will cause memory leak...

flighting
  • 15
  • 5
  • 2
    So, before the dialog is destroyed, you need to close worker thread. This is usually done by setting event and waiting for thread handle. Thread must periodically check the event state and exit immediately when event is signaled. – Alex F Sep 10 '13 at 07:08
  • 2
    Perhaps you shouldn't use member variables in the dialog class for the worker thread, then. Separate GUI and logic. – molbdnilo Sep 10 '13 at 07:18

1 Answers1

0

You should terminate the workthread safely before the maindlg is destroyed.

Here is the answer [Terminating a thread gracefully not using TerminateThread() ]

Community
  • 1
  • 1
CodeJuan
  • 46
  • 3
  • I try that method before, somehow i don't know what is wrong, the main thread will stuck in the loop of WaitForSingleObject method and never go the execute the worker thread... I don't know why, So i quite... – flighting Sep 17 '13 at 01:24
  • Is there Sleep() in the WaitLoop? – CodeJuan Sep 29 '13 at 02:47