Delphi Event-based and Asynchronous Programming
Book information
Description
Event-based programming is everywhere. Nowadays, you can hardly write any kind of application without leaning on events and messages.This simple, yet extremely powerful mechanism is also the cornerstone of asynchronous and multithreaded programming. Without events, we would not know when some task was completed.But, asynchronous and multithreaded programming consists of more than just handling multiple threads, protecting shared resources, and synchronization. It also includes designing and understanding program flow. That design aspect is often forgotten, taken for granted, and solving all the fine-grained nuances of multithreaded programming hogs the spotlight.Without understanding asynchronous flow and the bigger picture it can be hard to properly solve all the other issues, including multithreading. What used to be plain spaghetti code, now becomes temporal spaghetti.You can lose control over your objects, have bits of code creating and releasing them all over the place, even without adding multithreading into the mix. Memory leaks, crashes, and holding references to dead data long after its destruction are perfectly possible even in a singlethreaded environment.While the starting point of this book is giving a proper introduction to event-based and asynchronous programming flow and design, it also provides the necessary implementation details, explanations and code examples needed to put those designs into practice. Introduction Acknowledgements Code Part 1. The Loop Hidden complexity Peeking at the console Main event loop Event-based programming Pushmi-pullyu When does pushing happen? What about polling? Game loop Part 2. Messaging Systems Windows Messaging System Windows Message Queue Posting and sending messages Message priority Custom messages Windows Messaging System in VCL Window recreation Window for non-visual components Message handler inheritance Custom messaging systems and patterns System.Messaging Part 3. Life, the Universe, and Everything Life, the Universe, and Everything Automatic memory management Interfaces Mixing object and interface references Storing an instance reference in a object reference Constructing an instance without assignment to any reference In-place construction in a const parameter Using an inline variable with type inference Interfaces and generics Class helpers Where class helpers help? Interfaces don't have helpers Generics don't have helpers Anonymous methods and lambdas What exactly is an anonymous method? What is it good for? How are anonymous methods implemented? Anonymous method variable capture Capturing and loops Anonymous method reference cycles Using weak references to break anonymous method reference cycles Part 4. Asynchronous Programming and Multithreading Asynchronous programming and multithreading Making the first dive… Application.ProcessMessages Application.ProcessMessages reentrancy problem Moving long operations to a background thread Using an anonymous thread Extending the TThread class Thread reentrancy OnTerminate event handler Handling thread exceptions Canceling the task Passing the data Pitfalls Asynchronous message dialog Tasks Thread pool Managing task lifetime Waiting for task Handling task exceptions Canceling the task Passing the data Parallel Join & For Task continuation Back to the Future What is a future There is no Future like your own Future Generic approach to future TValue approach to future Part 5. Thread safety What is thread safety anyway? Wrong assumptions Working example of thread-unsafe code The importance of thread safety Achieving thread safety General thread safety considerations Memory allocation and deallocation Object construction and destruction Assignment thread safety What does atomic mean? Atomic does not imply thread-safe Reference assignments Use immutable data Avoiding shared data Use only local data Use thread-local data Initialization pattern Protecting shared data Synchronization primitives (objects) Thread synchronization Locks No lock Recursive lock Exclusive lock Readers-writer or multi-reader lock Spinlocks Monitor Events Atomic (interlocked) operations Part 6. GUI and Multithreading Main (GUI) thread Communicating with the main thread Synchronization with the main thread Updating UI after the task is completed Updating progress counter or description Iteratively creating and populating GUI controls while task is running Speeding up GUI controls Using messaging systems Using System.Messaging Using a thread-safe messaging system Communication and GUI issues Deadlocking the main thread Deadlock with two shared resources Deadlock with shared resource and Synchronize Deadlock with waiting for a thread and Synchronize Cleanup on GUI destruction Don't destroy the GUI Use guardian interface to prevent access to destroyed GUI Using messaging systems Waiting for a thread Final notes Appendix References Quality Portal Reports
Similar books
MySQL® Notes for Professionals book
2018 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
Session C11: Ancient Cultural Landscapes in South Europe – their Ecological Setting and Evolution, Session C22: Gardeners from South America, Session S04: Agro-Pastoralism and Early Metallurgy Sessions, Session WS29: The Idea of Enclosure in Recent Iberian Prehistory, Session C88: Rhytmes et causalites des dynamiques de l'anthropisation en Europe entre 6500 ET 500 BC: Hypotheses socio-culturelles et/ou climatiques: Proceedings of the XV UISPP World Congress (Lisbon 4-9 September 2006) / Actes du XV Congrès Mondial (Lisbonne 4-9 Septembre 2006) Vol.36
2010 · PDF
THE BRITISH ARMY IN INDIA: ITS PRESERVATION BY AN APPROPRIATE CLOTHING, HOUSING, LOCATING, RECREATIVE EMPLOYMENT, AND HOPEFUL ENCOURAGEMENT OF THE TROOPS. with AN APPENDIX ON INDIA : THE CLIMATE OP ITS HILLS ; THE DEVELOPMENT OF ITS RESODRCBS, INDUSTRY, AND ARTS ; THE ADMINISTRATION OF JUSTICE ; THE BLACK ACT ; THE PROGRESS OF CHRISTIANITY ; THE TRAFFIC IN OPIUM ; THE VALUE OF INDIA ; PERMANENT CAUSES OF DISAFFECTION, AND OF THE RECENT REBELLION ; THE TRADITIONARY POLICY; MISGOVERNMENT BY NATIVE RULERS ; ANNEXATIONS OF THEIR TERRITORY, ETC.
1858 · PDF
Idries Shah 27 Books Collection : A Perfumed Scorpion, A Veiled Gazelle, Caravan of Dreams, Darkest England, Destination Mecca, Evenings with Idries Shah, Knowing How to Know, Learning How to Learn, Letters and Lectures of Idries Shah, Neglected aspects of Sufi study, Observations, Oriental Magic, Reflections, Seeker after Truth, Special Illumination, Special Problems in the study of Sufi ideas, Sufi thought and action, Tales of the Dervishes, The Dermis Probe, The Elephant in the Dark, The Englishman Handbook, Idries Shah Antology, The Magic Monastery, The natives are restless, wisdom of the Idiots PDF.
2022 · PDF
The travels of Capts. Lewis and Clarke from St. Louis, by way of the Missouri and Columbia rivers, to the Pacific ocean; performed in the years 1804, 1805 & 1806, by order of the government of the United States. Containing delineations of the manners, customs, religion, &c. of the Indians, comp. from various authentic sources, and original documents, and a summary of the Statistical view of the Indian nations, from the official communication of Meriwether Lewis. Illustrated with a map of the country, inhabited by the western tribes of Indians
1809 · PDF
Professional Linux kernel architecture ''Wrox programmer to programmer''--Cover. - ''What you are reading right now is the result of an evolution over more than seven years: After two years of writing, the first edition was published in German by Carl Hanser Verlag in 2003. It then described kernel 2.6.0. The test was used as a basis for the low-level design documentation for the EAL4+ security evaluation of Red Hat Enterprise Linux 5, requiring to update it to kernel 2.6.18 (if the EAL acronym does not mean anything to you, then Wikipedia is once more your friend). Hewlett-Packard sponsored the translation into English and has, thankfully, granted the rights to publish the result. Updates to kernel 2.6.24 were then performed specifically for this book''--P. ix
2008 · PDF