Delphi's Multi-threading program

Source: Internet
Author: User

Turn from: http://www.cnblogs.com/gzcszzx/archive/2011/07/19/2110675.html Thank you

Delphi Multi-Threading Programming (a) the content of this article is taken from the network, and re-organized, here is only to facilitate their own learning and review. All code is tested in person and DELPHI7 under test is valid. Pictures are made for themselves.
Multithreading should be the basic skills of programmers, but this foundation I have never learned, so it just seems to be some, understand the times, actually I do not know.
The beginning should be a voluminous text, but I still advocate to do it first, in the attempt to understand. Try this first:
procedureTform1.button1click (Sender:tobject);
var
I:integer;
begin
forI: =0  to  500000  do     
begin       
Canvas.textout (10,  10, inttostr (i);    
Span style= "color: #000080;" > end
end
When the above program runs, our form is basically "dead", you can try to drag the form while you are running the program ... Delphi provides us with an easy way (application.processmessages) to solve this problem:
procedureTform1.button1click (Sender:tobject);
var
I:integer;
begin
forI: =0  to  500000  do     
begin       
Canvas.textout (10,     10, inttostr (i);      
application.processmessages;    
end ;  
end
This application.processmessages; Typically used in a more time-consuming loop, it checks for and processes other messages in the message queue first.
But this is not a multi-threaded, for example: running when you drag the form, the loop will pause ...
Before using multithreading, let's start by simply modifying the program:
functionMyfun:integer;
var
I:integer;
Begin

forI: =0 to500000 Do
begin
Form1.Canvas.Lock;
Form1.Canvas.TextOut (10,IntToStr (i));
Form1.Canvas.Unlock;
End;
Result: =0;
End;
procedure
  Tform1.button1click (sender: tobject);  Begin     
myfun; 
end
;  

Number of changes in the above program:
1, this is not multi-threaded, but also let the form false "dead" a while;
2, writes the execution code in a function, but this function does not belong to the TForm1 method, therefore uses the Canvas to be crowned the name (FORM1);
3, since it is a function, (whether or not necessary) should have a return value, 4, used 500,001 times Lock and Unlock.

canvas.lock is like saying: "Canvas (drawing surface) is busy, others want to use canvas, etc;" Canvas.unlock: Run out, unlock!
It's a good habit to use Lock and Unlock in the Canvas, but it doesn't matter if you don't use multithreading, but it's not guaranteed that the program will be extended to multi-threaded; We are now learning multi-threading, which of course should be used.
There are two ways to use multithreading in Delphi: Calling the API, using the TThread class, and using the API's code more easily.
functionMyfun (p:pointer): Integer; stdcall;
var

I:integer;
begin
forI: =0 to500000 Do
Begin

Form1.Canvas.Lock;
Form1.Canvas.TextOut (10,IntToStr (i));
Form1.Canvas.Unlock;
End;
Result: =0;
End;

procedure   Tform1.button1click (sender: tobject); 
var
    
id: thandle; 
begin     
CreateThread ( nil ,  0,  @MyFun,   Span style= "color: #000080;" >nil ,  0, id); 
end
  Code Analysis: CreateThread a thread, calculate the original main thread, so that the program has two threads, is the standard multithreading; CreateThread The third argument is the function pointer, which executes immediately after the new thread is established, and the execution of the function completes and the system destroys the thread to end the multithreaded story. The function to be used by the 
CreateThread is system-level, cannot be a method of a class (for example, TForm1), and has strict formatting (parameters, return values) required, whether or not you need to be in the format for the time being, because it is a system-level call, but also to prefix stdcall, StdCall is the coordination parameter order, although there is only one parameter in the order, but this is the practice of using system functions. The
CreateThread also requires a var parameter to accept the ID of the new thread, although it is temporarily useless, but this is also the format;
One of the simplest multithreaded routines comes out, let's do it again with the TThread class
type
Tmythread =class(TThread)
protected
procedureExecute; Override
End
;

procedureTmythread.execute;
var

I:integer;
begin
Freeonterminate: = True;{This allows the thread to be freed after execution is complete}
for
I: =0 to500000 Do
begin
Form1.Canvas.Lock;
Form1.Canvas.TextOut (10,  10, inttostr (i);      
Form1.Canvas.Unlock;     
end ;  
end ;   

procedure   Tform1.button1click (sender: tobject); 
begin
    
Tmythread.create (False); 
end
 < Span class= "Pas__keyword" >tthread class has an abstract method (Execute), and thus is an abstract class, the abstract class can only inherit the use, The above is inherited as Tmythread. The 
Inheritance TThread is primarily the implementation of the abstract method execute (which writes our code inside), and when our tmythread is instantiated, the code in the Execute method is executed first.
We usually instantiate this as usual:
   procedure    tform1.button1click (sender: tobject);  
var     
mythread: tmythread; 
begin     
mythread :=  Tmythread.create (False); 
end
因为 MyThread 变量在这里毫无用处(并且编译器还有提示), 所以不如直接写做 TMyThread.Create(False);
我们还可以轻松解决一个问题, 如果: TMyThread.Create(True) ? 这样线程建立后就不会立即调用 Execute, 可以在需要的时候再用 Resume 方法执行线程, 譬如:
procedureTform1.button1click (Sender:tobject);
var
Mythread:tmythread;
begin
MyThread: = Tmythread.create (True);
Mythread.resume;
End;
Can be simplified to:
procedure Tform1.button1click (sender:tobject);
begin
with Tmythread.create (True) do Resume;
End;

Delphi's Multi-threading program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.