Image Processing With ImageEn With A Secondary Thread

Multi-Threading
Multi-threading in Delphi lets you create applications that include several simultaneous paths of execution or processes.  A "normal" Delphi application is single-threaded: all (VCL) objects access their properties and execute their methods within this single thread usually referred to as the main thread.

Secondary Thread
To speed up data processing in your application you can decide to include one or more "secondary" threads.  When several threads are running in the application a question arises: how to update your GUI (user interface) as a result of a thread execution.  

Maintain An Active GUI
The answer to the question lies in the TThread class Synchronize method.  To update your application's user interface run in the main thread from a secondary thread you need to call the TThread.Synchronize method - a thread-safe method that avoids multi-threading conflicts that can arise from accessing object properties or methods that are not thread-safe, or using resources not in the main thread of execution. 

An active GUI is maintained in this demo while doing processor intensive image processing in the background.  Each image processing procedure is executed in a secondary thread.  In a non-threaded Delphi application a procedure may take several seconds to complete depending on the size of the image file and the processor used.  While the non-threaded image processing procedures are running the application would act like the GUI is locked.  It would be impossible to cancel the operation or resize the form because the GUI would be unresponsive to button clicks, key presses or mouse actions.  The same procedures processed in a secondary thread allow the GUI to remain responsive and not interfere with the image processing.

This demo was developed by Adirondack Software & Graphics for the “A Programmers Introduction To Using The ImageEn Image Library” EBook.  You may order the EBook at http://www.imageen.com/order/index.html.  If you already have subscribed to the EBook but do not have the demo, email me at w2m@frontiernet.net and we will send you a copy.