AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

multithread

Ein Thema von question · begonnen am 5. Okt 2013 · letzter Beitrag vom 7. Okt 2013
Antwort Antwort
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#1

multithread

  Alt 5. Okt 2013, 21:28
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
  Mit Zitat antworten Zitat
Namenloser

Registriert seit: 7. Jun 2006
Ort: Karlsruhe
3.724 Beiträge
 
FreePascal / Lazarus
 
#2

AW: multithread

  Alt 5. Okt 2013, 21:49
Have you looked at the Thread Sorting Demo that comes with Delphi?
  Mit Zitat antworten Zitat
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#3

AW: multithread

  Alt 5. Okt 2013, 23:35
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 ?
  Mit Zitat antworten Zitat
Namenloser

Registriert seit: 7. Jun 2006
Ort: Karlsruhe
3.724 Beiträge
 
FreePascal / Lazarus
 
#4

AW: multithread

  Alt 6. Okt 2013, 00:19
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

Geändert von Namenloser ( 6. Okt 2013 um 00:33 Uhr)
  Mit Zitat antworten Zitat
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#5

AW: multithread

  Alt 6. Okt 2013, 10:34
Hi Philip,

Thank you very much for your help, it was very helpful
  Mit Zitat antworten Zitat
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#6

AW: multithread

  Alt 6. Okt 2013, 17:34
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?
  Mit Zitat antworten Zitat
Medium

Registriert seit: 23. Jan 2008
3.686 Beiträge
 
Delphi 2007 Enterprise
 
#7

AW: multithread

  Alt 7. Okt 2013, 04:48
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!
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#8

AW: multithread

  Alt 7. Okt 2013, 07:50
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.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:52 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