🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.delphi.nativeapi
Article #12128

Re: starting with threads

#12128
From: Martin James
Date: Sun, 24 Nov 2002 13:25
85 lines
3533 bytes
Harri <harri@uninet.ee> wrote in message
news:3de0c520$1@newsgroups.borland.com...
> Hi!
>
> I modified ibx threaded query example (from codecentral)
> a little, but it is still not what I would like.
>
> ThreadQuery = class(TThread)
>   private
>     dmThread: TdmThread; // dynamically created datamodule
>  ...
> end;
>
> var
>   thdQuery : ThreadQuery;
>
> and in other unit :
> ..
> thdQuery := ThreadQuery.Create(..
> ..
>
> Thread is set "FreeOnTerminate := True" in constructor
> and has "dmThread.Free" inside destructor as in example.
> I'm not creating any other components dynamically.
>
> Now, if close main form, when thread is running, I get lots
> of AV-s. I managed to create "running threads count"
> variable, what forbids program exit, if threads count is >0.
> but this is really a temporary solution. In delphi threads
> example one can close main form when sort threads are running.
>
> I'm too stupid in this area to create thread pool so I thought
> it would be possible to do something like:
> var
>   thdQuery1, thdQuery2, thdQuery3 : ThreadQuery;

If you are creating & destroying the threads, rather than re-using
them, you do not need a threadPool.  You can easily however, store
all the current thread instances in a TThreadList:

currentLiveQueryThreads:TThreadList;

Add new ThreadQuery instances to the list in the constructor.  Remove
the threads from the list before exiting the thread.  If the list is
not a global, pass it to the constructor as a parameter & save it in
a field of the thread class so that the thread has access.  If you
want to terminate all the threads, lock the list & iterate it, call
the terminate method for each thread & unlock the list.

> 1) how can I set thdQuery2 to nil from thread, when it exits

No need, just lock the list, find 'self', remove, unlock, exit.  All
gone.

> 2) is it legal to set  thdQuery2.Terminated := True; even if
> thread is set to autoterminate

with the TThreadList, it is not possible for the 'autoterminate'
worker threads to exit while you have the list locked - they will get
stuck until you have finished setting the terminated properties of
all the threads to 'true', (by calling terminate), and so you cannot
attempt to terminate a thread that is already freed - nice and safe.

Rgds,
Martin




> 3) what should happen, when I close main form - are threads
> terminated automatically or they continue run to end.
>
> best regards,
> Harri
>
>
>
>
>
>
>
>
>
>
Technical Details
Message-ID: <3de0d40b@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!proxad.net!fr.usenet-edu.net!usenet-edu.net!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail
References: <3de0c520$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f94dd,b96fe3fe7bf7dbad
X-Google-Attributes: gidf94dd,public
X-Google-ArrivalTime: 2002-11-24 06:15:48 PST
From: "Martin James" <mjames_falcon@dial.pipex.com>
Newsgroups: borland.public.delphi.nativeapi
References: <3de0c520$1@newsgroups.borland.com>
Subject: Re: starting with threads
Date: Sun, 24 Nov 2002 13:25:54 -0000
Lines: 87
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2615.200
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
NNTP-Posting-Host: 62.188.19.249
Message-ID: <3de0d40b@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1038144523 62.188.19.249 (24 Nov 2002 05:28:43 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!proxad.net!fr.usenet-edu.net!usenet-edu.net!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail