Click here to Skip to main content
15,889,992 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Guidelines on ActiveX with a web page Pin
dabs13-Oct-05 8:58
dabs13-Oct-05 8:58 
AnswerRe: Guidelines on ActiveX with a web page Pin
Trollslayer13-Oct-05 9:23
mentorTrollslayer13-Oct-05 9:23 
QuestionSet client rectangle Pin
johnnyXP7-Sep-05 0:35
johnnyXP7-Sep-05 0:35 
AnswerRe: Set client rectangle Pin
Bob Ciora7-Sep-05 0:53
Bob Ciora7-Sep-05 0:53 
AnswerRe: Set client rectangle Pin
David Crow7-Sep-05 3:11
David Crow7-Sep-05 3:11 
QuestionMS Word automation Pin
Ahsan Askare7-Sep-05 0:27
Ahsan Askare7-Sep-05 0:27 
QuestionToolbar Background Color Pin
Identity Undisclosed7-Sep-05 0:27
Identity Undisclosed7-Sep-05 0:27 
QuestionDraw icon next to edit control in combo box Pin
Skute7-Sep-05 0:15
Skute7-Sep-05 0:15 
Questionctrl->alt by hooking Pin
szeszike6-Sep-05 23:25
szeszike6-Sep-05 23:25 
QuestionHow to monitor internet activities Pin
rajeevktripathi6-Sep-05 23:06
rajeevktripathi6-Sep-05 23:06 
AnswerRe: How to monitor internet activities Pin
David Crow7-Sep-05 3:14
David Crow7-Sep-05 3:14 
AnswerRe: How to monitor internet activities Pin
IndianOcean7-Sep-05 3:42
IndianOcean7-Sep-05 3:42 
Questionsimplest thread program doesn't work Pin
Spiritofamerica6-Sep-05 22:40
Spiritofamerica6-Sep-05 22:40 
AnswerRe: simplest thread program doesn't work Pin
Cedric Moonen6-Sep-05 22:57
Cedric Moonen6-Sep-05 22:57 
AnswerRe: simplest thread program doesn't work Pin
Hans Ruck6-Sep-05 22:58
Hans Ruck6-Sep-05 22:58 
AnswerRe: simplest thread program doesn't work Pin
Marc Soleda6-Sep-05 23:06
Marc Soleda6-Sep-05 23:06 
AnswerRe: simplest thread program doesn't work Pin
Bob Ciora7-Sep-05 3:03
Bob Ciora7-Sep-05 3:03 
AnswerRe: simplest thread program doesn't work Pin
LighthouseJ7-Sep-05 13:58
LighthouseJ7-Sep-05 13:58 
I agree with cedric. I recommend you look into events, they help control synchronization between the thread and your app.

Here's what I'd usually do:

struct threadPkg {<br />
  HANDLE hStartup, hStopNow, hShutdown;<br />
};<br />
<br />
UINT ThreadFunc (LPVOID lpParam) {<br />
  threadPkg * pkg = (threadPkg *) lpParam;<br />
  ::SetEvent(pkg->hStartup);<br />
<br />
  while (true) {<br />
    switch (::WaitForSingleObject(pkg->hStopNow, 10)) {<br />
      case WAIT_OBJECT_0: {<br />
        ::SetEvent(pkg->hShutdown);<br />
        AfxEndThread(0, true);<br />
        return 0;<br />
          } break;<br />
      case WAIT_TIMEOUT: {<br />
        // do some work here<br />
          } break;<br />
      }<br />
   }<br />
}<br />
<br />
bool StartThread (threadPkg * pkg) {<br />
  CWinThread * thread = AfxBeginThread(ThreadFunc, pkg, THREAD_PRIORITY_IDLE, 0, 0, NULL);<br />
<br />
  switch (::WaitForSingleObject(pkg->hStartup, INFINITE)) {<br />
    case WAIT_OBJECT_0: { return true; } break;<br />
    case WAIT_TIMEOUT: {    // should never be reached, INFINITE is a very long time,<br />
    return false;        // but I like to totally define all possible program paths<br />
        } break;<br />
    }<br />
}<br />
<br />
void StopThread (threadPkg * pkg) {<br />
  ::SetEvent(pkg->hStopNow);<br />
  switch (::WaitForSingleEvent(pkg->hShutdown, INFINITE)) {<br />
    case WAIT_OBJECT_0: break;<br />
  }<br />
}<br />
<br />
void main () {<br />
  threadPkg * pkg = new threadPkg;<br />
  pkg->hStartup = ::CreateEvent(NULL, true, false, "startup sequence, thread->main");<br />
  pkg->hStopNow = ::CreateEvent(NULL, true, false, "signal to stop now, main->thread");<br />
  pkg->hShutdown = ::CreateEvent(NULL, true, false, "thread has shut down, thread->main");<br />
<br />
  if (StartThread(pkg)) {<br />
    // thread is running, do what I need<br />
    StopThread(pkg);<br />
  } else {<br />
    MessageBox(NULL, "Error: The worker thread was not started.", "Critical Error", MB_OK);<br />
  }<br />
}


What that will do is begin a handshaking process when you start up the thread. This function creates 3 events and do what the text description says. It creates the thread, then waits for the thread to tell it that it has started and is on it's feet. You can do what you want and the thread will continue working, occaisionally asking if it's been told to quit. When you are ready to stop the thread, the function is called and it sends the hStopNow singal, the thread catches it, quits nicely and simultaneously signals back it's received and has shutdown. This is the basic organization of my threads I use all the time. Of course, what happens in main depends on what you want, you can put the start and end thread functions however you need them.

Note: I just wrote that off the cuff, I haven't tested that exact code but it looks correct in it's entirety.

Good Luck
Questionopening file by double-clicking (FileDDE) broken in VS 7.1 ?! Pin
T.T.H.6-Sep-05 22:36
T.T.H.6-Sep-05 22:36 
Questionstringsend? Pin
Member 21610046-Sep-05 22:31
Member 21610046-Sep-05 22:31 
AnswerRe: stringsend? Pin
Cedric Moonen6-Sep-05 23:13
Cedric Moonen6-Sep-05 23:13 
GeneralRe: stringsend? Pin
Member 21610046-Sep-05 23:39
Member 21610046-Sep-05 23:39 
GeneralRe: stringsend? Pin
Cedric Moonen6-Sep-05 23:45
Cedric Moonen6-Sep-05 23:45 
GeneralRe: stringsend? Pin
Member 21610047-Sep-05 17:20
Member 21610047-Sep-05 17:20 
AnswerRe: stringsend? Pin
kakan7-Sep-05 0:14
professionalkakan7-Sep-05 0:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.