Threaded Applications - PowerPoint PPT Presentation

About This Presentation
Title:

Threaded Applications

Description:

Threaded Applications Introducing additional threads in a Delphi application is easy Threaded Applications A Thread is? Why would you introduce ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 12
Provided by: Roger320
Category:

less

Transcript and Presenter's Notes

Title: Threaded Applications


1
Threaded Applications
  • Introducing additional threads in a Delphi
    application is ltRelativelygt easy

2
Threaded Applications
  • A Thread is?
  • Why would you introduce additional threads?
  • Basic Thread Code
  • Variables and Thread Safe Code
  • Some Thread Models - Communication

3
What is a Thread ?
  • Simple Program Sequential Steps
  • Waits to read data
  • Single Thread
  • Windows OS runs programs at the same time
  • Time allocation, System Waits
  • The OS also allows sections of a program to run
    independently in the same way.
  • Each TThread Object introduces an additional
    independent set of processor steps (thread)
    within the application.

4
Why Introduce Multiple Threads?
  • Users are no longer prepared to wait
  • If more than (?)lt1-2 Secondsgt then Thread
  • More efficient
  • Actions happen in other Thread wait cycles
  • Many things lend themselves to Background
    running
  • Large database reports
  • Internet access
  • Can make things simpler
  • Less need for state engines

5
Basic Thread Code
  • Inherit from TThread Object
  • Override the Execute Routine.
  • Handle exceptions within Execute
  • Keep Execute Thread safe
  • Must free TThread Object or the Application will
    not close
  • Must allow Execute to terminate

6
Thread Safety
  • Code is unsafe when a variable can be changed by
    separate threads in an uncontrolled manner and
    the change affects the program flow.
  • Procedure Local Variables are safe
  • Object Private and Protected variables and
    objects can be managed
  • Thread Local gtgt threadvar X Integer
  • Global Variable Equivalent ???

7
Synchronization Objects
  • Used to manage access to variables
  • TCriticalSection
  • Protect blocks of code
  • Acquire
  • Release
  • TSimpleEvent
  • One Thread initiates action then waits until
    other thread has set the event

8
Synchronize Routine
  • Only works with VCL thread
  • You get to pass a procedure of object without
    parameters
  • The procedure needs to be able to establish its
    context to access variables
  • The calling thread is suspended until the
    procedure returns

9
Thread Communication Models
  • Separate (console) process
  • Single Run
  • Recover Results in OnTerminate
  • Server with Post Messages
  • Post results home
  • Server with Synchronized callback
  • Call VCL Thread with results
  • Multiplexed Server
  • Wait for thread to complete action

10
Threads to Multiplex
Listen on Port
Spawn session
Handle Queries In order
Database Access
IP Session
Sql Query
Quey And Wait
Sql Results
11
  • Slides and demonstration code
  • http//www.innovasolutions.com.au/delphistuf/ADUGJ
    anuary2006.htm
Write a Comment
User Comments (0)
About PowerShow.com