delphi threads teaching II: thread space-time user interface (UI)

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

Section II: Operation time and space in the thread interface (UI)
 
1. Why TThread?
 
TThread-based operating system thread function package, hides a lot of tedious details.
Most suitable for realizing multi-threaded tasks. For this reason enough, right?
what? You use the windows api thread to achieve multi-threaded?
I can responsibly tell you, if you use the api to achieve multi-threaded task,
Coupled you gifted delphi object-oriented thinking to grasp very quickly,
So eventually you will write a similar thing with TThread to improve development efficiency.
Why toss it? To believe delphi engineer, people have already seen through everything. Ahem.
Similarly, to believe that Microsoft engineers, windows operating system is not what the big problem.
More Similarly, to believe that the phone design engineers, no film, they finally put before the phone thinner.
Haha, pull away. . .
(This tutorial default operating system is windows 7/10, delphi version is XE8, most of the code can run on the XE2)
 
2. Thread spacetime user interface (UI) in the end what doorway?
 
Many tutorials have repeatedly stressed that thread and space, are not allowed to go directly to update the UI, but does not seem to explain why.
We assume that UI interface allows multiple threads at the same time to update, and see what happens.
If two threads simultaneously proceed in the same area of ​​the interface drawing operation, such as to draw a green one to red,
So ultimately, it is not likely a big painted face appears on the screen?
It can be so simple to understand, you know why UI does not allow multi-thread to operate. Not can not, it is a last resort.
(Thread does not allow direct manipulation of UI, the same applies in Andrews)
 
3. TThread.Synchronize() 原理。
 
SendMessage function is, send a message to the window WM_NULL.
After receiving the news window to update the interface. Window messages in response to events can understand the main thread and space.
 
The following are examples of connected sections, the calculation results window on how to properly display.
 
unit  Unit10;
interface
uses
   Winapi . Windows, Winapi . Messages, System . SysUtils, System . Variants, System . Classes,  Graphics,
   Vcl . Controls, Vcl . Forms, Vcl . Dialogs, uAccumulation, Vcl . StdCtrls;
type
   TForm10 =  class (TForm)
     Edit1: TEdit;
     Button1: TButton;
     procedure  Button1Click(Sender: TObject);
   private
     procedure  OnAccumulated(Sender: TAccumulationThread);
   end ;
 
implementation
{ $R  *.dfm}
 
procedure  TForm10 . Button1Click(Sender: TObject);
var
   accThread: TAccumulationThread;
begin
   accThread := TAccumulationThread . Create( true );
   accThread . OnAccumulated := self . OnAccumulated;  //指定事件。
   accThread . FreeOnTerminate :=  true // 线程结束后自动释放
   accThread . Num :=  100 ;
   accThread . Start;
end ;
 
procedure  TForm10 . OnAccumulated(Sender: TAccumulationThread);
begin
   // 这里是线程时空
   // 要更新 UI ,要用 Synchorinize 把更新的操作
   // 塞到主线程时空里去运行。注意理解:“塞!”
   TThread . Synchronize( nil ,
     procedure
     begin
       // 这里的代码被塞到主线程时空里去了。
       Edit1 . Text := inttostr(Sender . Total);
     end );
   // Synchronize 第一个参数是 nil
   // 第二个参数是一个匿名函数 什么是匿名函数? 以后会介绍到。
end ;
end .
 
unit uAccumulation;
interface
uses
  Classes;
type
  TAccumulationThread = class; // This is stated in advance
  TOnAccumulated = procedure(Sender: TAccumulationThread) of object;
  // If you do not declare in advance, Sender will be defined as TObject
  // event function, it is necessary to operate Sender cast
  TAccumulationThread = class(TThread)
  protected
    procedure Execute; override;
  public
    Did he sound;
    Total: integer;
    OnAccumulated: TOnAccumulated;
  end;
 
implementation
 
procedure TAccumulationThread.Execute;
where
  i: integer;
begin
  Total := 0;
  if Num > 0 then
  begin
    for i := 1 to Num do
      Total := Total + i
  end;
  // When the calculations are complete, call OnAccumulated inform the caller
  if Assigned(OnAccumulated) then
    OnAccumulated(self);
end;
end.

  

 
 4. What space-time code running in the thread?
 
Execute function running, calling code, are "threaded code." Regardless of the code writing position !!!
Sysnchronize is a special existence, it may be time and space in the thread, the main thread code is stuffed into time and space to go running.
 
第三节,将实现线程如何保持生命力,创建后可以反复使用。慢慢进入实用阶段了,请不要错过。

Guess you like

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