Article View: borland.public.cppbuilder.nativeapi
Article #8735Thread WaitFor() ....waits forever
From: Daniel Stevenson
Date: Wed, 22 Sep 2004 05:08
Date: Wed, 22 Sep 2004 05:08
123 lines
3956 bytes
3956 bytes
I've created a fairly simple thread, but when it comes to closing down the
thread it won't terminate or it causes an Access Violation.
My Thread is as follows:
void __fastcall MyThread::Execute()
{
SetName();
try
{
Synchronize(PollServerService); //check when first started
while (!MyThread::Terminated)
{
Sleep(1000);
Synchronize(PollServerService);
}
}
catch (Exception &e)
{
ShowMessage(e.Message);
}
}
//--------------------------------------------------------------------------
-
void __fastcall MyThread::PollServerService()
{
bool Error = false;
try
{
//do something here
}
catch (Exception &e)
{
MyForm1->lblServiceConnection->Caption = "Not Found";
MyForm1->shpServiceConnection->Brush->Color = clRed;
MyForm1->lblServiceConnection->Properties->Transparent = true;
Error = true;
}
if (!Error)
{
MyForm1->lblServiceConnection->Caption = "Active";
MyForm1->shpServiceConnection->Brush->Color = clBtnFace;
MyForm1->lblServiceConnection->Properties->Transparent = false;
}
}
//--------------------------------------------------------------------------
-
void __fastcall MyThread::SetName()
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = "MyThread";
info.dwThreadID = -1;
info.dwFlags = 0;
__try
{
RaiseException( 0x406D1388, 0,
sizeof(info)/sizeof(DWORD),(DWORD*)&info );
}
__except (EXCEPTION_CONTINUE_EXECUTION)
{
}
}
//--------------------------------------------------------------------------
-
void __fastcall MyThread::TerminateAndWaitFor()
{
HANDLE hThread;
Suspend();
Terminate();
hThread = (void *)Handle;
Resume();
WaitForSingleObject(hThread, INFINITE);
}
//--------------------------------------------------------------------------
-
I construct the thread in a form MyFrom as follows:
__fastcall TMyForm1::TMyForm1(TComponent* Owner)
: TForm(Owner)
{
//do other stuff here..
Thread1 = new MyThread(false);
Thread1->FreeOnTerminate = true;
Thread1->Priority = tpLowest;
}
//--------------------------------------------------------------------------
-
The MyThread pointer is declared in MyForm1's header
MyThread *Thread1;
Seeing as MyForm1 is a sub form of another form, I am attempting to
terminate the thread from MyForm1's parent form using the following code:
MyForm1->Thread1->TerminateAndWaitFor();
delete MyForm1;
MyForm1 = NULL;
It all seems to compile and work well except when I attempt to terminate my
application. Upon terminating, the thread's call to
WaitForSingleObject(hThread, INFINITE); will just sit there forever and not
do anything.
If I change the INFINITE to a set value, it will move beyond the
WaitForSingleObject(hThread, INFINITE); but will throw an Access Violation
within rtl60.bpl
if I change the FreeOnTerminate to false it will still hang at
WaitForSingleObject(hThread, INFINITE); and not move past this.
Can anyone help?
Technical Details
Message-ID:
<415108cd$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,1897390e79ed181f,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Daniel Stevenson" <d.stevenson@ocreage.com.au> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Thread WaitFor() ....waits forever Date: Wed, 22 Sep 2004 15:08:30 +1000 Lines: 127 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: 202.174.228.150 Message-ID: <415108cd$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1095829709 202.174.228.150 (21 Sep 2004 22:08:29 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail