|
 |
 |
::
::
|
|
|
MultiThread Delphi Component
TMultiThread is a Delphi component for use in Win32 (9x/ME/2K/XP/Vista/W7) and x64 software.
It's a non visual class that helps creating multiple threads for example for creating a multi threaded thumbnailer or any other task that needs many threads.
You specify a max. thread count and simply add work to the class and when the work is ready a callback event is called with the result.
TMultiThread is optimized to be used for many threads of the same type but also simplyfies to run a single thread only.
No Synchronize() is used at all, so it is also safe to use the component in a .dll.
Installation
Unpack the package (TMultiThread x.x.zip).
- Delphi 7:
- In Delphi menu: Component: Install component
- Unit file name: select "MultiThread.pas"
- Compile and save package.
- Delphi 2009:
- Menu: File/New/Package Delphi
- In project manager (in the upper-right corner by default), right-click on "Package1.dproj" and select "Add..."
- Unit file name: click "Browse" and select "MultiThread.pas" and click "Ok"
- In project manager (in the upper-right corner by default), right-click on "Package1.dproj" and select "Install..."
- Save the package.
If everything went smooth, you can access the TMultiThread component on 3delite's tab.
Usage
Drop a TMultiThread component on the form. Create a class that will describe the job and the particular parameters for it. There is also a global
Parameters class that you can assign your own class to it that is global parameters for all the jobs (threads).
Run the component with MultiThread1.Start, a control thread will start and wait for the jobs.
Use MultiThread1.AddWork(Work) to add a job to the TMultiThread component. When this function is called a job is sent to the control thread and
as soon as there are free thread slots available it will be started.
Implement a ThreadWorkerCallback event. When a job is ready to run, this event will be called. You get the global Parameters class and the job
class here. Type-cast both to your class and you can extract the information describing the work to do. After the work is finished if you need to
send back results, then define a Results class and assign it to Results. You will get back the Results class in the ThreadResultsCallback event.
Be carefull not to use VCL stuff inside the ThreadWorkerCallback as the VCL is not thread-safe! If you want to for example send progress
information from here, you can use the built in message function (see the tutorial program's ThreadWorkerCallback event) or in your Data
implementation class or global Parameters class define a HWND handle of your main form, assign it the main form's handle and use PostMessage()
inside ThreadWorkerCallback to this handle to send any information you want. It's a good solution to define a class with the needed information,
send it's address with PostMessage() and process the variables in the main form's WndProc, then free the class.
Implement a ThreadResultsCallback event if you need some kind of results from the threads. When a thread finishes this event is called with your
ready results. If you stoped the TMultiThread component before this event was called with Stop then this event will be called only for the
already running threads.
When you are done or want to interrupt the threads call MultiThread1.Stop. No new threads will be started but existing threads will finish their
work and call the ThreadResultsCallback function. If you want to complety stop all running threads call StopAllThreads.
Properties
- AutoFreeData: Automatically free the Data object after leaving ThreadResultsCallback() event or when a thread is terminated.
- AutoFreeResults: Automatically free the Results object after leaving ThreadResultsCallback() event or when a thread is terminated.
- AutoFreeMessages: Automatically free the Message objects after leaving ThreadMessage() event.
- AutoFreeParameters: Automatically free the Parameters objects when freeing the TMultiThread component.
- ThreadCount:
- tcAutomatic: Automatically fires at most "number of CPU cores count" threads.
- tcManual: Manually set the maximum number of threads to fire at a time. Use ThreadCountManual property to set the max. thread count.
- ThreadCountManual: Only used if tcManual is set. Specify maximum threads to run at the same time.
- ThreadPriority: The spawned threads priority. Note that the control thread always runs at tpNormal.
- Active: True if .Start has been called and control thread is running and ready to accept jobs, False otherwise.
Methodes
- Start: Run the control thread and start accepting jobs.
- Stop: Stop the control thread. No new threads will be run. Already running threads will continue to run.
- StopAllThreads: Stop all running threads, clear the job (work) list, no new threads will be started.
Inside the ThreadWorkerCallback check the thread's .Terminated property to get notified about a thread termination.
Also you have to free the Data class if you detect a terminate and AutoFreeData is False.
Events
- ThreadMessage: If message is sent from the thread with the thread's SendMessage(Message1, Message2: TObject) function it arrives here.
- Sender: The TMultiThread class that calls this event.
- var Message1: The class that was sent as Message1.
- var Message2: The class that was sent as Message2.
Do not forget to free the classes before exiting this event if AutoFreeMessages is False!
- ThreadResultsCallback: When a thread finishes it's work this event is called.
- Sender: The TMultiThread class that calls this event.
- var Data: The Data that was given as the work. Use this class to identify the job.
- var Results: If Results was assigned to a class, you get the assigned Results class here.
Do not forget to free the classes before exiting this event if AutoFreeData and/or AutoFreeResults is False!
- ThreadWorkerCallback: This is the function that will be runned inside a thread.
Be carefull what you do as for example VCL is not accessible here, so do not modify VCL controls inside here. All things that apply for threads in general applies here too.
- Thread: The thread class the function is called in.
- Parameters: If you specified a Parameters class for the TMultiThread component you get the Parameters class here.
- var Data: The Data that was given as the work. Use this class to identify the job, type-cast it to your own class implemented.
- var Results: Is nil by default, create a class and assign it to Results after the work has been done. You will get this class back in ThreadResultsCallback event.
TMultiThread in shareware and commercial software?
You can use this component in your free programs for FREE. You have to buy a license if you want to use it in a shareware or commercial software.
Useful information
|
|
|
|
|
|