Click here to Skip to main content
16,011,170 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CDialog::OnInitDialog () Assertion Error Pin
Michael P Butler29-Sep-01 2:08
Michael P Butler29-Sep-01 2:08 
Question‘CdialogBar’? Pin
Ahmad28-Sep-01 22:30
Ahmad28-Sep-01 22:30 
GeneralA simple Callback Howto, please Pin
yarp28-Sep-01 21:39
yarp28-Sep-01 21:39 
General.bmp to .jpg Pin
Maya C28-Sep-01 20:17
Maya C28-Sep-01 20:17 
GeneralRe: .bmp to .jpg Pin
Christian Graus29-Sep-01 21:02
protectorChristian Graus29-Sep-01 21:02 
GeneralAll or nothing Pin
overflow28-Sep-01 17:49
overflow28-Sep-01 17:49 
GeneralRe: All or nothing Pin
Michael Dunn28-Sep-01 20:40
sitebuilderMichael Dunn28-Sep-01 20:40 
GeneralRe: All or nothing Pin
29-Sep-01 2:27
suss29-Sep-01 2:27 
One solution might be to introduce an Event synchronization object into your program. You can associate one event object with every thread, and make it set by default. The last line in the thread's function (before it exits and the thread ends) would be "WaitForSingleObject(hThreadEvent, INFINITE)". Since the event is SET by default, most times the thread will just continue - and terminate.

Now, wherever you have a bunch of lines such as
::GetExitCodeThread(...)
...

Just reset the event object that is associated with the thread in question. Something like:

ResetEvent(pThread->hEvent);
::GetExitCodeThread(pThread->hThread, &dwExitCode);
if (dwExitCode == STILL_ACTIVE) {
pThread->SuspendThread();
}
SetEvent(pThread->hEvent);

Now, after writing that, I wonder what would happen if the thread function will PASS the WaitForSingleObject, and then a context switch will take place, and only then your ResetEvent(pThread->hEvent) will execute. In such a case, I guess your problem still exists.

So let's change that solution a little. What if, instead of depending on the thread's exit code returned by the OS, you'll manage that yourself? Again, an event object will be associated with each thread. When the thread starts executing, it will RESET the event.As long as the event is RESET - the application can know that the thread is still executing. I added a critical section there - because otherwise under certain circumstances a cotnext switch would have still caused problems.


Thread:
-------
ResetEvent(hEvent)
... thread stuff ...
...
... Want to exit ...
EnterCriticalSection(&csThread)
SetEvent(hEvent)
LeaveCriticalSection(&csThread)

App:
----
EnterCriticalSection(&csThread)
if (TIMEOUT == WaitForSingleObject(hEvent, 0))
{
// Thread is still executing
pThread->SuspendThread()
}
LeaveCriticalSection(&csThread)

And now that I think about it - adding a critical section to the previous idea should also fix it, so you can choose any of the two.

Good luck...
GeneralRe: All or nothing Pin
overflow3-Oct-01 18:49
overflow3-Oct-01 18:49 
GeneralQuestion about GetControlBar() Pin
overflow28-Sep-01 15:42
overflow28-Sep-01 15:42 
QuestionHow can I show/hide the plus symbol of a tree item? Pin
28-Sep-01 15:34
suss28-Sep-01 15:34 
AnswerRe: How can I show/hide the plus symbol of a tree item? Pin
Michael Dunn28-Sep-01 16:20
sitebuilderMichael Dunn28-Sep-01 16:20 
AnswerRe: How can I show/hide the plus symbol of a tree item? Pin
Paolo Messina29-Sep-01 2:42
professionalPaolo Messina29-Sep-01 2:42 
QuestionHow to send an e-mail using OnFileSendMail Pin
28-Sep-01 15:13
suss28-Sep-01 15:13 
AnswerRe: How to send an e-mail using OnFileSendMail Pin
Shog929-Sep-01 10:02
sitebuilderShog929-Sep-01 10:02 
QuestionHow to let a control support OLE Pin
28-Sep-01 15:04
suss28-Sep-01 15:04 
AnswerRe: How to let a control support OLE Pin
Michael Dunn28-Sep-01 16:22
sitebuilderMichael Dunn28-Sep-01 16:22 
QuestionHow to get hInternet handle from within IE ? Pin
Peter Hayward28-Sep-01 14:45
Peter Hayward28-Sep-01 14:45 
GeneralAVI & BMP Pin
28-Sep-01 12:26
suss28-Sep-01 12:26 
GeneralMSVCRT... Pin
28-Sep-01 11:51
suss28-Sep-01 11:51 
GeneralRe: MSVCRT... Pin
Walter Sullivan28-Sep-01 14:05
Walter Sullivan28-Sep-01 14:05 
GeneralRe: MSVCRT... Pin
28-Sep-01 14:37
suss28-Sep-01 14:37 
Generalkeybd_event Pin
Mustafa Demirhan28-Sep-01 10:12
Mustafa Demirhan28-Sep-01 10:12 
GeneralRe: keybd_event Pin
Paolo Messina29-Sep-01 0:24
professionalPaolo Messina29-Sep-01 0:24 
GeneralWake the computer from standby Pin
Mustafa Demirhan28-Sep-01 10:09
Mustafa Demirhan28-Sep-01 10:09 

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.