Board index » delphi » Threads and Objects

Threads and Objects

Hello:

  2 questions (for the same application I'm writing).

The program sends out pages via a user interface and/or
through OLE automation.

1. When a call is added to the calling queue (which for this case is
    nothing more than a string grid) a thread is started (if not
    already running; I.E. There is a single thread for ALL calls).
    This thread scans through the string grid looking for new calls.
    Calls already dialed are marked as such.  I would like it to
    periodically clean up the string grid for visual purposes (getting
    rid of the calls that have been already dialed or have been
    cancelled by the user).  The problem here is that the user has
    the ability to clean-up the calls by pressing a button or may be
    in the process of adding a call to the grid at the exact time that
    the independant thread is cleaning up the queue.  I'm afraid that
    this may cause some sort of clash?  How is this typically handled?
    Should I just set some sort of flag when cleaning up/done cleaning
    up.  Is there a better way to handle this?

2. When adding calls to the same string grid as above; the following
    type of thing would occur in code:

    frmMain.grdQueue.Cells[0,CurrentRow]:=DateTimeToStr(Now);
    frmMain.SetCallStatus(CurrentRow,InitialState);
    frmMain.grdQueue.Cells[2,CurrentRow]:=Caller;
    { blah, blah, blah }

    { It's this line I'm concerned with: }
    frmMain.grdQueue.Objects[0,CurrentRow]:=TObject(LongInt(LineID));

    Now, when I later clean-up the grid (blanking old objects,
    shrinking the grid, etc.), I have to explicitly delete the object?
    Even though it's only a long integer and not explicitly allocated
    by me?  Is this correct?

Thanks for any help!   :-)

------------------------------------------------------
Anything I have ever said or will ever say are my own
opinions and not necessarily those of my company.

Michael J. Loeffler
Teletrade, Inc.

mloeff...@teletrade.com
------------------------------------------------------

 

Re:Threads and Objects


On Tue, 18 Mar 1997 15:41:58 GMT, mloeff...@teletrade.com (Michael J.

Quote
Loeffler) wrote:
>1. When a call is added to the calling queue (which for this case is
>    nothing more than a string grid) a thread is started (if not
>    already running; I.E. There is a single thread for ALL calls).
>    This thread scans through the string grid looking for new calls.
>    Calls already dialed are marked as such.  I would like it to
>    periodically clean up the string grid for visual purposes (getting
>    rid of the calls that have been already dialed or have been
>    cancelled by the user).  The problem here is that the user has
>    the ability to clean-up the calls by pressing a button or may be
>    in the process of adding a call to the grid at the exact time that
>    the independant thread is cleaning up the queue.  I'm afraid that
>    this may cause some sort of clash?  How is this typically handled?
>    Should I just set some sort of flag when cleaning up/done cleaning
>    up.  Is there a better way to handle this?

You are right to be concerned. Use TThread.Synchronize or Windows
critical sections.

Quote
>2. When adding calls to the same string grid as above; the following
>    type of thing would occur in code:

>    frmMain.grdQueue.Cells[0,CurrentRow]:=DateTimeToStr(Now);
>    frmMain.SetCallStatus(CurrentRow,InitialState);
>    frmMain.grdQueue.Cells[2,CurrentRow]:=Caller;
>    { blah, blah, blah }

>    { It's this line I'm concerned with: }
>    frmMain.grdQueue.Objects[0,CurrentRow]:=TObject(LongInt(LineID));

>    Now, when I later clean-up the grid (blanking old objects,
>    shrinking the grid, etc.), I have to explicitly delete the object?
Wrong.
>    Even though it's only a long integer and not explicitly allocated
>    by me?  

Right.

The "Object" is a just an integer, so there is nothing to delete/free.

--
Ray Lischner, Tempest Software, Inc., Corvallis, Oregon, USA
Author of Secrets of Delphi 2 (http://www.tempest-sw.com/secrets/)

Re:Threads and Objects


Quote
Michael J. Loeffler wrote:

> Hello:

>   2 questions (for the same application I'm writing).

> The program sends out pages via a user interface and/or
> through OLE automation.

> 1. When a call is added to the calling queue (which for this case is
>     nothing more than a string grid) a thread is started (if not
>     already running; I.E. There is a single thread for ALL calls).
>     This thread scans through the string grid looking for new calls.
>     Calls already dialed are marked as such.  I would like it to
>     periodically clean up the string grid for visual purposes (getting
>     rid of the calls that have been already dialed or have been
>     cancelled by the user).  The problem here is that the user has
>     the ability to clean-up the calls by pressing a button or may be
>     in the process of adding a call to the grid at the exact time that
>     the independant thread is cleaning up the queue.  I'm afraid that
>     this may cause some sort of clash?  How is this typically handled?
>     Should I just set some sort of flag when cleaning up/done cleaning
>     up.  Is there a better way to handle this?

> 2. When adding calls to the same string grid as above; the following
>     type of thing would occur in code:

>     frmMain.grdQueue.Cells[0,CurrentRow]:=DateTimeToStr(Now);
>     frmMain.SetCallStatus(CurrentRow,InitialState);
>     frmMain.grdQueue.Cells[2,CurrentRow]:=Caller;
>     { blah, blah, blah }

>     { It's this line I'm concerned with: }
>     frmMain.grdQueue.Objects[0,CurrentRow]:=TObject(LongInt(LineID));

>     Now, when I later clean-up the grid (blanking old objects,
>     shrinking the grid, etc.), I have to explicitly delete the object?
>     Even though it's only a long integer and not explicitly allocated
>     by me?  Is this correct?

> Thanks for any help!   :-)

1) Use a mutex. Look up CreateMutex in Win API help. Also have a look at
WaitForSingleObject and the rest of that family. (Mutex = MUTual
EXclusion, if I remember correctly)

2) As you don't allocate any memory, you don't have to deallocate any.
Ignore the Objects[x, y] when cleaning the list. You're just setting the
value of a pointer which the stringgrid maintains. If you had created an
object, the stringgrid still would treat the Objects[x, y] as what it
is, a pointer to something, and you would have to free the memory
pointed to by Objects[x, y]

Regards,

Erik.

--
Need a custom component? Late on a project? Could use an util?
DOS Device driver? A VxD? NT drivers or services?
Applications of any kind?
Low rates, fast delivery!

When responding to news postings, please CC a copy to my email address.
Thanks.
Erik Sperling Johansen <e...@info-pro.no>

Re:Threads and Objects


mloeff...@teletrade.com (Michael J. Loeffler) wrote:

Quote
>Hello:

>  2 questions (for the same application I'm writing).

>The program sends out pages via a user interface and/or
>through OLE automation.

>1. When a call is added to the calling queue (which for this case is
>    nothing more than a string grid) a thread is started (if not
>    already running; I.E. There is a single thread for ALL calls).
>    This thread scans through the string grid looking for new calls.
>    Calls already dialed are marked as such.  I would like it to
>    periodically clean up the string grid for visual purposes (getting
>    rid of the calls that have been already dialed or have been
>    cancelled by the user).  The problem here is that the user has
>    the ability to clean-up the calls by pressing a button or may be
>    in the process of adding a call to the grid at the exact time that
>    the independant thread is cleaning up the queue.  I'm afraid that
>    this may cause some sort of clash?  How is this typically handled?
>    Should I just set some sort of flag when cleaning up/done cleaning
>    up.  Is there a better way to handle this?

If you want to make sure simply UNassign the event handler for the button
temporarily, e.g:

  SaveClick := TheUserButton.OnClick;
  TheUserButton.OnClick := nil;
  { process }
  TheUserButton.OnClick := SaveClick;

Preferably you would assign a different event handler telling the user to be
patient <g> rather than giving no feedback

Quote
>2. When adding calls to the same string grid as above; the following
>    type of thing would occur in code:

>    frmMain.grdQueue.Cells[0,CurrentRow]:=DateTimeToStr(Now);
>    frmMain.SetCallStatus(CurrentRow,InitialState);
>    frmMain.grdQueue.Cells[2,CurrentRow]:=Caller;
>    { blah, blah, blah }

>    { It's this line I'm concerned with: }
>    frmMain.grdQueue.Objects[0,CurrentRow]:=TObject(LongInt(LineID));

>    Now, when I later clean-up the grid (blanking old objects,
>    shrinking the grid, etc.), I have to explicitly delete the object?
>    Even though it's only a long integer and not explicitly allocated
>    by me?  Is this correct?

No. No need to. What you are doing actually is to assign a 4-byte value to a
field in [0,CurrentRow]. Delphi does not care whether this is a TObject (a
pointer) or a longint. It is taken as it is, no memory is allocated and thus
there is no need to fiddle with that.

--
Stefan.Hoffmeis...@UNI-Passau.DE
http://kakadu.rz.uni-passau.de/~w4hoff01/
University of Passau, Bavaria, Germany

Other Threads