Hello experts.
Please help me.
Alloc me only 10 minutes, please.
please write me a simple delphi application that works with many threads.
I need the program scheleton.
But i dont know where to put my own procedure in thread.execute or in button.click?
please write me a simple app that works with threads (number of threads must be indicated in edit), and tell me where to put my procedure...
And tell me where to put my procedure (this connect to a site and parse a text) in thread.execute or button.click..
Please, everyone help me!
Thanks
You need to understand the code for Delphi threading, before you can make your own, just I was before. Even now I strive hard to fully understand threading in Delphi. Not like .Net is easy to implement.
Ok;
Look at the sample codes I send to you, compare code lines.
Follow some steps here;
and understand it loudly
<from Freddy1990>
The easiest way is sublassing the TThread class.
uses Classes;
type
TMyThread = class(TThread)
protected
procedure Execute; override;
public
constructor Create;
destructor Destroy; override;
end;
...
procedure TMyThread.Create;
begin
inherited Create(False);
FreeOnTerminate := True;
end;
This example would start the thread instantly after creation and free it automatically on termination. You just put the code you want to execute in the Execute method.
If you want to interact with the main thread, you need to synchronize calls by adding a method to the thread without result or parameters and calling Synchronize on it, like Synchronize(Method);
how many processors do you have in your computer ?
if you have only 1, then there is almost no gain,
except for a more responsive front-end, the actual work will still take the same amount of time
if you have no knowledge about threading in delphi,
then start reading about the basics :
starting a thread, using protection objects to access shared resources, ending/cleaning up threads
There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.
yes, i make it all steps..
the thread.execute section is thread instructions
and the button1.click is the creation of thread.
When i debug it, i'm getting an error : ...check your template
and nothing happens..
Its structure is quite simple -- and simple is good
In most components there are only a few procedures and properties you need to think about; this is not an exception with TThread. The only methods you'll need to worry about are Execute, Create, and Synchronize; and the only property that you'll usually need to access is FreeOnTerminate.
Key Methods and Properties of TThread
The key methods and property of TThread are listed below in Table 1.
The Create method allocates memory, starts the thread and specifies the thread function in CreateThread as the Execute procedure. Here, as in any Create method, you can initialize vars and perform some preliminary operations. However, unlike a normal Create method, the thread is already executing by the time the method ends. Usually this isn't a problem because you'll just create the object and forget about it. But if you have to do any processing before the thread starts executing, set the CreateSuspended parameter to False. This allocates memory for the thread and sets it up, but the thread will not execute until you make a call to the Resume procedure. This is useful, especially if you need to set up values your thread will need over the course of its lifetime.
Attribute: Execute Parameters: None
Execute is your TThread's central execution method. It's fairly synonymous with a main processing or central control procedure or function you might use to execute all the procedures in a particular unit. The only difference is that with a TThread object, the first method that is called must be called Execute. This doesn't mean that you can't call another procedure which acts in that capacity from Execute. It's definitely more convenient to put your execution code here because that's Execute's purpose.
One important note: If you look in the object declaration of TThread, you'll see that it is declared as a virtual; abstract; method. This means it's not implemented in any way, shape or form; therefore, it's up to you to provide the code. And there's no inherited functionality, so you'll never make a call to inherited Execute; in your own implementation.
Synchronize is your key to safely accessing the VCL in your thread. When Synchronize executes, your thread becomes a part of the main thread of the program, and in the process, suspends the operation of the main thread. This means the VCL can't receive messages from other threads other than the one you're synchronizing to the main thread, which in turn makes it safe to execute any VCL calls.
Think of Synchronize as a middleman in a transaction. You have a buyer, which is the main thread of the program, and a seller of services, which is another thread of execution created within the same process. The seller would like to sell the buyer some goods -- in our case, do some operation on the VCL components running in the main thread -- but the buyer doesn't really know the seller, and is appalled at how the seller actually performs his service, so is afraid the seller's service may have a negative effect on him. So the seller enlists the help of an agent (the Synchronize procedure) to smooth things out, take the buyer out to lunch so the seller can do his thing.
The seller is a procedure that performs the action on behalf of the thread. It doesn't have to be a specific type, but it must be a method of the thread. Say I have a long process running in a background thread that at certain times must update the text in a TPanel on the main form to give feedback about the thread's current status to the user. If I wrote in my code Form1.Panel1.Caption := 'This is my update message', I'd raise an exception -- most likely an access violation error. But if I encapsulate the call in a procedure, then call Synchronize, the message will be sent and text will be updated. Synchronize acted as a middleman so my status message could be passed to the TPanel.
I've may have confused rather than enlightened you! Just remember this:
When you want to call VCL objects from another thread, create a wrapper procedure for the operations you want to carry out, then use Synchronize to synchronize your thread with the main thread so your call can be safely executed.
Synchronize is meant to be used really quickly. Execute it, then get out of it as soon as you can. While Synchronize is running, you essentially have only one thread working. The main thread is suspended.
Attribute: FreeOnTerminate Parameters: Set property to Boolean value
This is a really useful property. Typically, once your execute procedure finishes, the Terminated Boolean property is set to False. However, the thread still exists. It's not running, but it's taking up space, and it's up to you to free the thread. But by setting this property in the Create constructor to True, the thread is immediately freed -- and with it, all the resources it took up -- after it's finished executing.
http://www.torry.net/samples/samples/os/ejemplodethreads.zip
Another one with details:
http://www.delphi3000.com/articles/article_2522.asp?SK=