Sunday, September 29, 2013

Basic Multithreaded Application In Delphi

Okey, On this article i will share about how to create a multithread application on Delphi if you dont know what is multithread you can read http://en.wikipedia.org/wiki/Multithreading_(software), On this tutorial i will use Delphi XE 2, but i think it can be implemented on any version of Delphi
First of all, Open your Delphi application and Create a VCL Application

Create 3 Progress Bar and 3 Button and just give it the default name


Now Create new TThread Class

Enter This Code Below TForm class

TDemoThread = class(TThread)
  private
    FProgress: TProgressBar;
  protected
    procedure Execute; override;
  public
    constructor Create(AProgress: TProgressBar);
  end;



And Enter this Code on implementation section

constructor TDemoThread.Create(AProgress: TProgressBar);
begin
  inherited Create(False);
  FreeOnTerminate := True;
  FProgress := AProgress;
  FProgress.Max := 100;
  FProgress.Position := 0;
end;

procedure TDemoThread.Execute;
begin
  while (True) do
  begin
    while FProgress.Position < FProgress.Max do
    begin
      FProgress.Position := FProgress.Position + 1;
      Sleep(100);
    end;
    FProgress.Position := 0;
  end;
end;


The constructor Create(AProgress:TProgressBar) is Constructor for the Thread
and procedure Execute;override method is procedure that will call automatic after creating the thread so the main thread function is must be placed on this method

Next we edit the code on button with function to call the thread
First of all we need a handle for the thread
And we create the thread
so our code on the Button will look like this
procedure TForm1.Button1Click(Sender: TObject);
var
  ProgressThread: TDemoThread;
begin
  ProgressThread := TDemoThread.Create(ProgressBar1);
end;
above code is for Button1 and will update ProgressBar1 add same code on other button but with different ProgressBar

So Our Full code will be like this


unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;

type

  TForm1 = class(TForm)
    ProgressBar1: TProgressBar;
    ProgressBar2: TProgressBar;
    ProgressBar3: TProgressBar;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TDemoThread = class(TThread)
  private
    FProgress: TProgressBar;
  protected
    procedure Execute; override;
  public
    constructor Create(AProgress: TProgressBar);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
constructor TDemoThread.Create(AProgress: TProgressBar);
begin
  inherited Create(False);
  FreeOnTerminate := True;
  FProgress := AProgress;
  FProgress.Max := 100;
  FProgress.Position := 0;
end;

procedure TDemoThread.Execute;
begin
  while (True) do
  begin
    while FProgress.Position < FProgress.Max do
    begin
      FProgress.Position := FProgress.Position + 1;
      Sleep(100);
    end;
    FProgress.Position := 0;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ProgressThread: TDemoThread;
begin
  ProgressThread := TDemoThread.Create(ProgressBar1);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  ProgressThread: TDemoThread;
begin
  ProgressThread := TDemoThread.Create(ProgressBar2);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  ProgressThread: TDemoThread;
begin
  ProgressThread := TDemoThread.Create(ProgressBar3);
end;

end.

Now compile and run your code and it will be like this

Thats All, our basic multithread application has finished

Note:above code are a bad example of updating VCL component, the proper way is use syncronize method that will execute code inside syncronize method on main thread, i will write article about syncronize method later.

No comments

Post a Comment

Powered by Blogger.