delphi Thread Teaching Section I: acquaintance multithreading

Reprinted from: https://www.cnblogs.com/lackey/p/6297115.html

Section I: acquaintance multithreading
 
1. Why learn multithreaded programming?
 
Multithreading (multiple threads to run concurrently) programming, also called asynchronous programming.
With multiple threads, the main interface will not time-consuming because of the code caused "suspended animation" state.
With multi-threaded, so multiple tasks can be performed simultaneously, maximizing the use of CPU resources and improve efficiency.
Andrews programming, requirements must be multi-threaded, so long as the main interface code takes a few seconds, it will trigger ANR error.
Multi-threaded programming work is the norm. Multithreading is necessary to master! The sooner the better!
 
delphi multithreading Demo misunderstanding on 2. Network
 
API is implemented using multiple threads. Too difficult, especially with a pointer to pass parameters, not suitable for beginners.
Interface with access to, for example, the starting point would be wrong. Multithreading is not good at operating UI.
Popular online demo is get hold of a cycle, again a TextOut output.
The results had to add canvas.Lock, is quite frustrating.
 
3. Definition of the main thread
 
Assume that an EXE program with a FrmMain (TForm).
FrmMain above a Button1 (TButton), a Edit1 (TEdit) and a Timer1 (TTimer).
So, we usually interface (UI) definition of the main thread that FrmMain is the main thread.
Button1 OnClick event in the space-time code to run on the main thread. (This tutorial threads are unified definition of the term space-time)
The input operation is the main thread of Edit1 temporal user.
OnTimer event of Timer1 code also runs on the main thread time and space.
Please note: the easiest for beginners to the OnTimer event mistaken for a multi-threaded space-time.
 
4. The difference between ordinary programming and multi-threaded programming
 
// 普通编程
function  Accumulate(num: integer ): integer ;
var
    i: integer ;
begin
    result:= 0 ;
    if  num< 1  then  exit;
    for  i:= 1  to  num  do
       result:=result+i;
end ;
// 在 Button1 的 OnClick 事件中编写如下代码:
var
    n,Total: integer ;
begin
    n:= 100 ;
    Total:=Accumelate(n);
    // 等待计算结果,假设计算需要5分钟,此处就得等待5分钟。
    // 这5分钟内,界面是无法访问的,是假死的。
    // 计算完成,得到结果 Total=5050;
    DoSomeThing;  //接着执行此句。
end ;
 
// 多线程编程,此为计算线程类
unit  uAccumulation;
interface
uses
   Classes;
type
   TAccumulationThread =  class ;
   TOnAccumulated =  procedure (Sender: TAccumulationThread)  of  object ;
   TAccumulationThread =  class (TThread)
   protected
     procedure  Execute; override;
   public
     Num:  integer ;
     Total:  integer ;
     OnAccumulated: TOnAccumulated;
   end ;
 
implementation
 
procedure  TAccumulationThread . Execute;
var
   i:  integer ;
begin
   inherited ;
   Total :=  0 ;
   if  Num >  0  then
   begin
     for  i :=  1  to  Num  do
       Total := Total + i
   end ;
   // 当计算完成后,就调用  OnAccumulated 通知调用者
   if  Assigned(OnAccumulated)  then
     OnAccumulated(self);
end ;
end .
 
Please write the name in English, with the right words, tenses. Remember, very important!
 
// 调用多线程
// 在FrmMain 中定义 OnAccumulated 事件函数
Procedure TFrmMain.OnAccumulated(Sender:TAccumulationThread);
var
   sum:integer;
begin
   // 当计算完成时,计算线程就调用本事件函数。
   // 我们在这里就得到了计算结果
   sum:=Sender.Total;
   // 因为这里是线程时空,不能直接把 sum 的值显示到界面上。
   // 如何正确显示,将下一章节中讲解。
end;
 
// 在Button1 的OnClick 事件中编写下面的代码
var
   thd:TAccumulationThread;
begin
   // 此处为主线程时空。
   thd:=TAccumuationThread.Create(true);
   thd.OnAccumulated=Self.OnAccumulated; // Self 指是 FrmMain.
   thd.Num:=100;
   thd.Start; //启动线程,在线程时空中执行 Execute 中的代码。
   //  start 立即返回并执行下一条代码 DoSomeThing;
   //  此时,就有两个线程在同时执行。
   //  1.主线程,也就是此处运行的时代码。
   //  2.计算线程,也就是 Execute 中的代码,这些代码此时运行于多线程时空。
   //  由于是两个线程并行在执行,故此处 DoSomeThing 马上可以执行。
   //  界面也不会假死
   DoSomeThing;
end;

  

 
通过比较,似乎看出多线程要写更多的代码?要用事件来传递结果来给调用者?
其实都不然,这两点均是面向对象(OO)写法,与线程类无关。
最大的不同是:异步执行,把耗时的计算任务放了入另一个线程时空!
 
多线程带来了效率,同时也带来了更多的麻烦。欲知后事如何,且听下回分解。

Guess you like

Origin www.cnblogs.com/approx/p/11852128.html