Better alternative to Application.ProcessMessages?

Posted on 2009-12-18
Last Modified: 2012-05-08
My first question, so I hope I am doing it correctly!  I'm not sure how much or how little detail to give...

I need some guidance - I have a list of tasks that must be performed in order. Each task instruction is sent to a separate worker thread for processing. The sequence must wait for the preceding task to complete before moving on to the next one. I currently have a flag set by the worker thread when the task is done, and the list waits for this flag in an "application.processmessages" loop, before moving on to the next task.  It works, but I can't help but feel there is a better way.  Can someone suggest a better approach?
Question by:GVP
    LVL 36

    Expert Comment

    by:Geert Gruwez
    the best is to not use it at all

    have you checkec omni thread library yet by delphi geek ?
    this has a system with worker threads and a system for ordering the threads executing
    LVL 12

    Accepted Solution

    I can think of two ways...
    There is an event in the TThread base class which is called OnTerminate... this event is triggered from the main thread, when the execute mehod of the thread finishes... hence, it is safe to place this eventhandler in the main form of your applicaiton... So, whenever the event fires, you can start start the next thread in the list.

    If your threads are not constructed in a way that makes them terminate when they are finished, but instead they just set a flag and continue ruinning, then you can acheive a similar functionality as in the first case by adding a TNotifyEvent to your thread class, along with a function to trigger this event... you then set an event handler in your main form, and whenever the thread is finished with it's calculations, you call the trigger function using the Synchronize procedure, which will make it execute from the main thread... the eventhandler itself will then start the next thread in the list...

    with these methods you never have to keep a loop in your main form that calls application.process messages, so you can leave that to the message pump...

    LVL 19

    Expert Comment

    Can't let the worker threads fire an event when they're done which you process in the main thread?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now