Click here to Skip to main content
15,881,089 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
QuestionAdding page number to word doc Pin
Barm6-Sep-05 21:46
Barm6-Sep-05 21:46 
QuestionChange Font of caption of dialog box ?? Pin
ana_v1236-Sep-05 20:59
ana_v1236-Sep-05 20:59 
AnswerRe: Change Font of caption of dialog box ?? Pin
Barm6-Sep-05 21:43
Barm6-Sep-05 21:43 
GeneralRe: Change Font of caption (title) of dialog box ?? Pin
Anonymous13-Sep-05 1:47
Anonymous13-Sep-05 1:47 
QuestionProper time to record Pin
LiYS6-Sep-05 20:20
LiYS6-Sep-05 20:20 
AnswerRe: Proper time to record Pin
David Crow7-Sep-05 3:22
David Crow7-Sep-05 3:22 
QuestionProblem related to CImageList(explanation) Pin
a_david1236-Sep-05 20:18
a_david1236-Sep-05 20:18 
QuestionHow does CListCtrl notify its parent window when a cell content has changed? Pin
followait6-Sep-05 20:18
followait6-Sep-05 20:18 
AnswerRe: How does CListCtrl notify its parent window when a cell content has changed? Pin
Ian Bowler7-Sep-05 13:39
Ian Bowler7-Sep-05 13:39 
GeneralRe: How does CListCtrl notify its parent window when a cell content has changed? Pin
followait7-Sep-05 16:15
followait7-Sep-05 16:15 

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.