http://www.thebits.org/tutorials/mjsema.asp
Guide: Workers Threads and Signals
© Malcolm Smith, 14th October 2002
Like all of our guides, this guide is not obligated to deal with anything that may happen at any time. Please see our legal information page. This article is based on projects tested under Windows XP Professional.
table of Contents
Author
page 1
Worker thread and semaphore
What is a worker thread?
page 1
Create a worker thread and wait for them to complete.
page 2
Summary
page 2
Download source code
Download source code
Author
Malcolm Smith (Malcolm@mjfreeelancing.com) - - Wener of Business MJ Freeelancing. Check Out
Products Available at www.mjfreeelancing.com.
What is a worker thread?
This article assumes that you already have the basic knowledge of how to create threads in C Builder and basic conditions such as executing the Execute method, run your thread code. Please read the relevant documentation of C Builder before entering this article.
You have tried to write a multi-threaded application, but it has found some horrified interruption when threaded tasks. If the answer is "Yes", then this article is prepared for you.
When the task in the processing is executed, the application can run the most common cause of 100% and your "multi-thread" application might create multi-threads, but they are actually related to the facts that run independently. This big error is to run other threads and interrupt the main VCL thread but fail to return the main thread and let the second thread run in its own space.
In order to achieve this paper, I can create a thread that iterate on files and folders. However, in order to make the thread principle as simple as possible, I will replace it with the creation of a specific value and update the tag on the form. This thread will run with the main VCL thread without obstacles (except for the call of the synchronous update tag).
Class Tworkerthread: Public TTHREAD
{
Private:
INT COUNTTO
Int counter;
TLABEL * Plabel;
protected:
Void __fastcall execute (void);
Void __fastcall UpdateLabel (Void);
PUBLIC:
__fastcall tworkerthread (Bool CreateSuspend)
TLABEL * Alabel, int ACOUNTTO;
}
The above thread class gets a pointer from a TLABEL.
The implementation of the thread is very simple.
__fastcall TWorkerThread :: TWorkerThread (bool CreateSuspended, TLabel * ALabel, int ACountTo): TThread (CreateSuspended), pLabel (ALabel) {Counter = 0; CountTo = ACountTo;} void __fastcall TWorkerThread :: Execute (void) {while (Terminated! && counter
The thread is executed, the internal record is set to 0, and the value to be calculated is thus initialized.
The demo program comes with this article has a section name "Creation of Simple Threads". The code for each button is similar to the following. TWORKERTHREAD * threeRead = New Torkerthread (True, Label1, 50);
Thread-> freeOnterminate = true;
Thread-> resume ();
This creates a new thread to pass an updated tag and a counter value. When the thread is Resumed, the tag's CAPTION displays an increased value. Click the same button again, you will see the label is updated by multi-thread (until the counter reaches its goals)
There is another button at the bottom of this group, which simulates all the press.
Ok, it is a simple thread that does not block the main VCL thread because every created thread is
Running independently.
Page 2 | Download source code
(Translation: 01soft)