🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: aborting a function

#14897
From: Clayton Arends
Date: Tue, 29 Jul 2003 22:58
98 lines
3890 bytes
"Robert L. Smith" <bsmithOMIT@cinSPAMele.com> wrote in message
news:3f26d058@newsgroups.borland.com...

> I need TalkBtnClick to wait for the value returned by talk_to_hardware
> before TalkBtnClick returns.  Or how do I "wait for" the thread to execute
> and "return" while simultaenously monitoring the abort button and allowing
> other TalkBtnClick's to fire?

I knew you were going to ask about this? ;-)

There are a few things you could do.  One way is to set up a callback event
handler scheme so that when talk_to_hardware() is finished the thread will
notify you of the result.  Here is how that might look (the "..." denotes
existing code from my previous posts):

  // thread.h

  typedef void (__closure *TTalkResultEvent)(int);
  class TalkHardwareThread : public TThread
  {
    ...
    private:
    ...

      int fResult;
      TTalkResultEvent fOnResult;
      void __fastcall SyncedResult();

    ...
    public:
    ...
      __property TTalkResultEvent OnResult =
        { read = fOnResult, write = fOnResult };
  };


  // thread.cpp

  void __fastcall TalkHardwareThread::SyncedResult()
  {
    fOnResult(fResult);
  }

  void __fastcall TalkHardwareThread::Execute()
  {
    ...
          fResult = talk_to_hardware();
          if (fOnResult)
            Synchronize(&SyncedResult);
    ...
  }


  // main.h

  class TForm1 : public TForm
  {
    ...
    private:
    ...
      void TalkToHardwareResult(int Result);
    ...
  };


  // main.cpp

  __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
  {
    fTalkHardwareThread = new TalkHardwareThread(false);
    fTalkHardwareThread->OnResult = &TalkToHardwareResult;
  }

  void TForm1::TalkToHardwareResult(int Result)
  {
    // do something with the result
  }

Some things to note:

- You don't have to synchronize the call to fOnResult but I figured I would
show it being done that way in case you wanted to make sure only one result
would come in at a time.  Also, if the result comes back synchronized then
you are free to perform whatever GUI operation you want to do with the
result.

- If you want a different handler for each result from talk_to_hardware()
then it is only marginally more work.  Instead of using 'fCallCount' in
TalkHardwareThread you could set up a list.  The list would store the event
handler to call and any parameters necessary to send to talk_to_hardware (if
any, which there is none in this example).  Whenever a call is made to
talk_to_hardware you would remove an element from the list and call the
result event handler.

Hopefully I have given you a clear enough example so that you can take it in
the direction you require.

- Clayton
Technical Details
Message-ID: <3f26fc14@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!news.tele.dk!news.tele.dk!small.news.tele.dk!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3f21a974@newsgroups.borland.com> <3f2232db$1@newsgroups.borland.com> <3f22abaf$1@newsgroups.borland.com> <3f26d058@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,cb957e45a585bf2c
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-07-29 16:10:16 PST
From: "Clayton Arends" <nospam_claytonarends@hotmail.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3f21a974@newsgroups.borland.com> <3f2232db$1@newsgroups.borland.com> <3f22abaf$1@newsgroups.borland.com> <3f26d058@newsgroups.borland.com>
Subject: Re: aborting a function
Date: Tue, 29 Jul 2003 15:58:33 -0700
Lines: 100
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: 24.30.128.202
Message-ID: <3f26fc14@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1059519508 24.30.128.202 (29 Jul 2003 15:58:28 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!news.tele.dk!news.tele.dk!small.news.tele.dk!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail