Article View: borland.public.cppbuilder.nativeapi
Article #11623Re: WaitForMultipleObjects in TThread
From: Remy Lebeau (TeamB)
Date: Mon, 02 Feb 2004 09:06
Date: Mon, 02 Feb 2004 09:06
48 lines
2666 bytes
2666 bytes
"Kristoffer Persson" <no@email> wrote in message
news:401e070a@newsgroups.borland.com...
> This looks like a good idea, but my threads usually wait in
> a GetMessage call.
You can change your code to use PeekMessage() instead. That will allow you
to check the message queue without blocking the thread so that you can then
performing timed waiting on the terminate event.
> Maybe a combination would be best, where I set an event
> and send a message to the threads.
No need for sending a message to terminate the threads, just set the event.
If you use MsgWaitForMultipleObjects(), it will tell you in a single
function call whether the terminate event was triggered or a new message was
received in the queue so that you can then call PeekMessage() to retreive
it. For example:
void __fastcall TMyThread::Execute()
{
MSG msg;
HANDLE hTerminateEvent = CreateEvent(NULL, TRUE, FALSE,
"MyTerminateEvent");
if( hTerminateEvent )
{
while( !Terminated )
{
switch( MsgWaitForMultipleObjects(1, &hTerminateEvent,
FALSE, 500, QS_POSTMESSAGE | QS_SENDMESSAGE) )
{
case WAIT_OBJECT_0:
Terminate();
break;
case (WAIT_OBJECT_0+1):
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
// handle message as needed...
break;
}
}
CloseHandle(hTerminateEvent);
}
}
Gambit
Technical Details
Message-ID:
<401e1181$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<40191ff7$1@newsgroups.borland.com> <1075389099.199618@post.sympatec.net> <401a0744$1@newsgroups.borland.com> <401c18c1@newsgroups.borland.com> <401e070a@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,2e632a3799f22e8b X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-02-02 01:09:03 PST From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <40191ff7$1@newsgroups.borland.com> <1075389099.199618@post.sympatec.net> <401a0744$1@newsgroups.borland.com> <401c18c1@newsgroups.borland.com> <401e070a@newsgroups.borland.com> Subject: Re: WaitForMultipleObjects in TThread Date: Mon, 2 Feb 2004 01:06:55 -0800 Lines: 50 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 67.22.138.253 Message-ID: <401e1181$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1075712385 67.22.138.253 (2 Feb 2004 00:59:45 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail