![]() ![]() The use of PostMessage is essential in this example. PostMessage(FReportTo, UM_WORKERDONE, Self.Handle, 0) The execute method does its calculation and then posts a message to the handle it received in its constructor to say its done: procedure TWorker.Execute This is essential as it will allow the main thread to query the thread instance for its result. The constructor just sets the private members and sets FreeOnTerminate to False. Thread typeĬonstructor Create(const aFactor: Double const aReportTo: THandle) As long as you keep it that way and don't start calling these methods from the context of different threads, there is no way for them to produce race conditions. Both methods only ever run in the context of the main thread. They are only modified in two locations: the StartThreads method and the HandleThreadResults method. In this example, as it stands, there is no need. You will need to decide whether you need to protect theses resource using locks or not. As is the use of a "Running" boolean to make it easier to determine whether there are still any threads that need to finish. This is a potential source of race conditions. When a thread is finished, the corresponding reference in the array gets nil-ed. References to the worker threads are kept in an array in the form. This example shows how you can do it using simple threads, allowing the GUI to access the thread instance after it is finished by setting FreeOnTerminate to false, and reporting when a thread is "done" using PostMessage. Never in the context of any other thread. That should always be done in the context of the main thread. Showing anything in the GUI requires interacting with controls and/or the message queue/pump. The fun starts when you want to make the GUI show intermediate and final results or report on the progress. Kicking off any number of threads to do some work usually isn't a problem. Keeping a GUI responsive while running a lengthy process requires either some very elaborate "callbacks" to allow the GUI to process its message queue, or the use of (background) (worker) threads. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |