🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #15219

Re: WaitForSingleObject()

#15219
From: Paul Ooi
Date: Sat, 12 Jul 2003 11:08
118 lines
3861 bytes
In a separate thread:

Brown *BThis = (Brown*)(lpParam);

 if (BThis)
 {
    HANDLE HNDConnectEvnt = BThis->BAMyAlpha->GetConnectEvntHandle();

    if (HNDConnectEvnt)
    {
        for (;;)
       {
            switch (WaitForSingleObject(HNDConnectEvnt, INFINITE))
            {
             case WAIT_OBJECT_0:
             {
                  ResetEvent(HNDConnectEvnt);

                  SOCKET SCKClient;
                  struct sockaddr_in SAClient;

                  BThis->BAMyAlpha->GetClient(0, &SCKClient, &SAClient);
                  BThis->CreateSelectThrd(SCKClient);

                  break;
             }

             case WAIT_FAILED:
                  return 0;
            }
       }
    }
}


In the primary thread:

When user closes the application, Brown is deleted.  In the destructor of
Brown:

Brown::~Brown()
{
     if (BAMyAlpha)
     {
          delete BAMyAlpha;
          BAMyAlpha = NULL;
     }

    if (HNDMonitorThrd)
    {
          switch (WaitForSingleObject(HNDMonitorThrd, INFINITE))
          {
               case WAIT_OBJECT_0:
                    CloseHandle(HNDMonitorThrd);
                    HNDMonitorThrd = NULL;
                    break;

               case WAIT_FAILED:
                    break;
          }
    }
}

In the destructor of BAlpha:

BAlpha::~BAlpha()
{
     if (HNDConnectEvnt)
     {
          CloseHandle(HNDConnectEvnt);
          HNDConnectEvnt = NULL;
     }
}

The creation of the event is done in the constructor of BAlpha, the
following function is called.

void BAlpha::CreateConnectEvnt()
{
     if (BASCurStat == BASReady)
     {
          HNDConnectEvnt = CreateEvent(NULL, TRUE, FALSE, NULL);

          if (HNDConnectEvnt == NULL)
               BASCurStat = BASErrConnectEvnt;
     }
}

Therefore when the application closes, I expect the handle to the event to
be closed during the destruction of BAlpha and
WaitForSingleObject(HNDConnectEvnt, INFINITE) in the separate thread to
return WAIT_FAILED, causing the thread to terminate.

But no, the thread is still stuck in WaitForSingleObject after the handle is
closed, why?  It works if I don't request WaitForSingleObject to wait
forever.

Paul


"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message
news:3f0fc71d$3@newsgroups.borland.com...
>
> "Paul Ooi" <yitung.ooi@student.unsw.edu.au> wrote in message
> news:3f0f7f61@newsgroups.borland.com...
> > I tried with default, it doesn't work.
>
> Please show your exact code.
>
> > Is it because I close the handle AFTER the wait function is called?
>
> As long as the function is still waiting at the time of the closure, then
> no.
>
>
> Gambit
>
>
Technical Details
Message-ID: <3f0fec06@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3f0f500e$2@newsgroups.borland.com> <mrtugv80rjdoc7ub6nca8i6e55jeaar0c8@4ax.com> <3f0f7f61@newsgroups.borland.com> <3f0fc71d$3@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,17beab47e7f815ec
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-07-12 04:09:13 PST
From: "Paul Ooi" <yitung.ooi@student.unsw.edu.au>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3f0f500e$2@newsgroups.borland.com> <mrtugv80rjdoc7ub6nca8i6e55jeaar0c8@4ax.com> <3f0f7f61@newsgroups.borland.com> <3f0fc71d$3@newsgroups.borland.com>
Subject: Re: WaitForSingleObject()
Date: Sat, 12 Jul 2003 19:08:03 +0800
Lines: 120
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: 129.94.198.109
Message-ID: <3f0fec06@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1058008070 129.94.198.109 (12 Jul 2003 04:07:50 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail