Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   multithread (https://www.delphipraxis.net/176945-multithread.html)

question 5. Okt 2013 20:28

multithread
 
Dear All,

I am trying to get clear idea about multithreading in Delphi, theoritically i have idea about it, i was looking for some simple practical example to try it out, i have found some sample code but somehow it is confusing to me.

can anybody give me a basic simple example of multithreading ? it would be great help for me

thanks in advance

Namenloser 5. Okt 2013 20:49

AW: multithread
 
Have you looked at the Thread Sorting Demo that comes with Delphi?

question 5. Okt 2013 22:35

AW: multithread
 
Thanks for the link, I have read this tutorial.

Actually, I would like to test a very simple example using multithreading, for example, I want to have three function "addition, subtraction and multiplication"

I would like to put this three functions into threads so that they can execute simultaneously,can anybody help me please ?

Namenloser 5. Okt 2013 23:19

AW: multithread
 
Is this simple enough?
Delphi-Quellcode:
unit Unit1;

{$IFDEF FPC}{$mode Delphi}{$ENDIF}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TMathThread }

  TMathThread = class(TThread)
  protected
    FValue: extended;
    FLabel: TLabel;
    procedure UpdateGUI;
  public
    constructor Create(OutputLabel: TLabel);
  end;

  { TAddThread }

  TAddThread = class(TMathThread)
    procedure Execute; override;
  end;

  { TMulThread }

  TMulThread = class(TMathThread)
    procedure Execute; override;
  end;

  { TSubThread }

  TSubThread = class(TMathThread)
    procedure Execute; override;
  end;

  { TfrmMain }

  TfrmMain = class(TForm)
    btnAdd: TButton;
    btnMul: TButton;
    btnSub: TButton;
    lblAdd: TLabel;
    lblMul: TLabel;
    lblSub: TLabel;
    procedure btnMulClick(Sender: TObject);
    procedure btnSubClick(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { private declarations }
    FAddThread, FMulThread, FSubThread: TMathThread;
  public
    { public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{ TfrmMain }

procedure TfrmMain.btnAddClick(Sender: TObject);
begin
  FAddThread.Resume;
end;

procedure TfrmMain.btnMulClick(Sender: TObject);
begin
  FMulThread.Resume;
end;

procedure TfrmMain.btnSubClick(Sender: TObject);
begin
  FSubThread.Resume;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  FAddThread := TAddThread.Create(lblAdd);
  FMulThread := TMulThread.Create(lblMul);
  FSubThread := TSubThread.Create(lblSub);
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  // TThread.Free calls TThread.Terminate internally and waits for the thread
  // to end
  FAddThread.Free;
  FMulThread.Free;
  FSubThread.Free;
end;

{$IFDEF FPC}
  {$R *.lfm}
{$ELSE}
  {$R *.dfm}
{$ENDIF}

{ TMathThread }

procedure TMathThread.UpdateGUI;
begin
  FLabel.Caption := FloatToStr(FValue);
end;

constructor TMathThread.Create(OutputLabel: TLabel);
begin
  // Create Suspended
  inherited Create(True);

  FLabel := OutputLabel;
  FValue := 1.0;
end;

{ TSubThread }

procedure TSubThread.Execute;
begin
  while not Terminated do
  begin
    FValue := FValue - 1;
    sleep(100);
    Synchronize(UpdateGUI);
  end;
end;

{ TMulThread }

procedure TMulThread.Execute;
begin
  while not Terminated do
  begin
    FValue := FValue * 1.01;
    sleep(100);
    Synchronize(UpdateGUI);
  end;
end;

{ TAddThread }

procedure TAddThread.Execute;
begin
  while not Terminated do
  begin
    FValue := FValue + 1;
    sleep(100);
    Synchronize(UpdateGUI);
  end;
end;

end.
(Sorry, I had to write this under Lazarus, as my Delphi installation now freezes on startup for some reason... I hope it compiles)


Edit:
I just noticed, the dfm file might be helpful...
Delphi-Quellcode:
object frmMain: TfrmMain
  Left = 408
  Height = 429
  Top = 261
  Width = 618
  Caption = 'frmMain'
  ClientHeight = 429
  ClientWidth = 618
  OnCreate = FormCreate
  object lblAdd: TLabel
    Left = 232
    Height = 16
    Top = 112
    Width = 36
    Caption = 'Start Add Thread'
    ParentColor = False
  end
  object lblSub: TLabel
    Left = 232
    Height = 16
    Top = 200
    Width = 34
    Caption = 'lblSub'
    ParentColor = False
  end
  object lblMul: TLabel
    Left = 232
    Height = 16
    Top = 158
    Width = 35
    Caption = 'Start Mul Thread'
    ParentColor = False
  end
  object btnAdd: TButton
    Left = 80
    Height = 25
    Top = 104
    Width = 131
    Caption = 'btnAdd'
    OnClick = btnAddClick
    TabOrder = 0
  end
  object btnMul: TButton
    Left = 80
    Height = 25
    Top = 151
    Width = 128
    Caption = 'btnMul'
    OnClick = btnMulClick
    TabOrder = 1
  end
  object btnSub: TButton
    Left = 80
    Height = 25
    Top = 190
    Width = 128
    Caption = 'Start Sub Thread'
    OnClick = btnSubClick
    TabOrder = 2
  end
end

question 6. Okt 2013 09:34

AW: multithread
 
Hi Philip,

Thank you very much for your help, it was very helpful

question 6. Okt 2013 16:34

AW: multithread
 
Hi,
I have two Queries to insert the values into Database, QueryA-->Insert Data to TableA, QueryB-->Insert Data to Table B.
both QueryA and QueryB are executed in one button click, Procedure TForm1.Button1Click(Sender:TObject);

now i would like to create threads for both query and shows the update in progressbar after Button1Click, i was trying since a long time, but still not sucess :(

could you please help me?

Medium 7. Okt 2013 03:48

AW: multithread
 
Well, what is your actual problem? Your description is very general at best, and without knowing any of your code (and DB libs), and not knowing what goes wrong where and what should happen instead... well I guess you know what I'm trying to say. Be more specific. A LOT!

Furtbichler 7. Okt 2013 06:50

AW: multithread
 
Long time ago there was a rule: new question => new thread.

But my advice to you is more simple: use google, find examples and ask, if you have a specific question.


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:36 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz