Book Image

Delphi Cookbook

By : Daniele Teti
Book Image

Delphi Cookbook

By: Daniele Teti

Overview of this book

Table of Contents (14 chapters)
Delphi Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Talking with the main thread using a thread-safe queue


Using a background thread and working with its private data is not difficult, but safely bringing information retrieved or elaborated by the thread back to the main thread to show them to the user (as you know, only the main thread can handle the GUI in VCL as well as in FireMonkey) can be a daunting task. An even more complex task would be establishing a generic communication between two or more background threads. In this recipe, you'll see how a background thread can talk to the main thread in a safe manner using the TThreadedQueue<T> class. The same concepts are valid for a communication between two or more background threads.

Getting ready

Let's talk about a scenario. You have to show data generated from some sort of device or subsystem, let's say a serial, a USB device, a query polling on the database data, or a TCP socket. You cannot simply wait for data using TTimer because this would freeze your GUI during the wait, and...