Application

Synchronize thread with application

Posted in Application

This is a simple example, which shows how to synchronize thread with application. In our example, we will get result of calculations in a main form. Use for this Synchronize method of TThread type.

type
  TTestThread = class(TThread)
  private
    j: Integer;
  protected
    procedure GetResult;
    procedure Execute; override;
  end;

...

procedure TTestThread.GetResult;
begin
  Form1.Caption:=IntToStr(j);
end;

procedure TTestThread.Execute;
var
  i: Integer;
begin
  j:=1;
  FreeOnTerminate:=True;
  for i:=1 to 90000000 do
  begin
    if Terminated then break;
    Inc(j, Round(Abs(Sin(Sqrt(i)))));
  end;
  Synchronize(GetResult);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  NewThread: TTestThread;
begin
  NewThread:=TTestThread.Create(False);
end;

Use ProcessMessages function

Posted in Application

ProcessMessages interrupts the execution of an application so that Windows can respond to events. So, if you will remove Application.ProcessMessages(); string from this example, then you will never see 'Some text' string on caption of the form.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Caption:='Some text';
  Application.ProcessMessages();
  Sleep(3000);
  Caption:='Form1';
end;

Synchronize thread with application

Posted in Application

This is a simple example, which shows how to synchronize thread with application. In our example, we will get result of calculations in a main form. Use for this Synchronize method of TThread type.

type
  TTestThread = class(TThread)
  private
    j: Integer;
  protected
    procedure GetResult;
    procedure Execute; override;
  end;

...

procedure TTestThread.GetResult;
begin
  Form1.Caption:=IntToStr(j);
end;

procedure TTestThread.Execute;
var
  i: Integer;
begin
  j:=1;
  FreeOnTerminate:=True;
  for i:=1 to 90000000 do
  begin
    if Terminated then break;
    Inc(j, Round(Abs(Sin(Sqrt(i)))));
  end;
  Synchronize(GetResult);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  NewThread: TTestThread;
begin
  NewThread:=TTestThread.Create(False);
end;
This is a simple example, which shows how to synchronize thread with application. In our example, we will get result of calculations in a main form.

Use for this Synchronize method of TThread type.


type
  TTestThread = class(TThread)
  private
    j: Integer;
  protected
    procedure GetResult;
    procedure Execute; override;
  end;

...

procedure TTestThread.GetResult;
begin
  Form1.Caption:=IntToStr(j);
end;

procedure TTestThread.Execute;
var
  i: Integer;
begin
  j:=1;
  FreeOnTerminate:=True;
  for i:=1 to 90000000 do
  begin
    if Terminated then break;
    Inc(j, Round(Abs(Sin(Sqrt(i)))));
  end;
  Synchronize(GetResult);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  NewThread: TTestThread;
begin
  NewThread:=TTestThread.Create(False);
end;

Use ProcessMessages function

Posted in Application

ProcessMessages interrupts the execution of an application so that Windows can respond to events. So, if you will remove Application.ProcessMessages(); string from this example, then you will never see 'Some text' string on caption of the form.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Caption:='Some text';
  Application.ProcessMessages();
  Sleep(3000);
  Caption:='Form1';
end;

Suspend/resume thread

Posted in Application

To set thread in suspend/resume status, use Suspend/Resume property of TThread type.

 

<pre>
type
  TTestThread = class(TThread)
  private
    j: Integer;
  protected
    procedure GetInfo;
    procedure Execute; override;  
  end;
...
var
  NewThread: TTestThread;
...

procedure TForm1.Button2Click(Sender: TObject);
begin
  NewThread.Resume;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  NewThread.Suspend;
end;
</pre>