AGB  ·  Datenschutz  ·  Impressum  







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

Basic TThread doesn't work

Ein Thema von FaNIX · begonnen am 1. Feb 2008 · letzter Beitrag vom 1. Feb 2008
Antwort Antwort
FaNIX

Registriert seit: 8. Okt 2007
36 Beiträge
 
#1

Basic TThread doesn't work

  Alt 1. Feb 2008, 07:55
Ok, I am trying to figure out how this TThread Class work, here is my example:

I am trying to test and see if a thread makes my main application responsive, so that it doesn't seem like it's not responding to events and clicks on the main form, so i created a TThread class, which does a maths calculation, this takes about 8 seconds to complete, giving me time to see if my main app is responsive, but it doesn't seem to work, what I am doing wrong?

My Main Form

Delphi-Quellcode:
unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, System.ComponentModel, Borland.Vcl.StdCtrls,Borland.Vcl.ComCtrls, Borland.Vcl.ExtCtrls,
  IdBaseComponent, IdThreadComponent;

type
  TForm3 = class(TForm)
    Button1: TButton;
    MainProgress: TProgressBar;
    IdThreadComponent1: TIdThreadComponent;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
      procedure MyOnTerminate(Sender:TObject);

  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

uses MyThread;


{$R *.nfm}
//*******************************************************//
procedure TForm3.Button1Click(Sender: TObject);
var
  TT : TMyThread;
begin

  TT := TMyThread.Create(True);
  TT.FreeOnTerminate := True;

  TT.OnTerminate := MyOnTerminate;
  TT.Resume;

end;
//*******************************************************//
procedure TForm3.MyOnTerminate(Sender: TObject);
begin
  ShowMessage('Done');
end;
//*******************************************************//
end.
My Thread Class:

Delphi-Quellcode:
unit MyThread;

interface

uses Classes,SysUtils,Dialogs,dte_Update_Library_DB,ADODB,ComCtrls;

type
   TMyThread = class(TThread)

  private
     procedure DoIt;

  protected
    procedure Execute; override;

end;

implementation

//*******************************************************//
procedure TMyThread.Execute;
begin
  synchronize(DoIt);
end;
//*******************************************************//
procedure TMyThread.DoIt;
var
  Count : double;
  i : integer;
begin
  for i := 0 to 900000000000000000 - 1 do
  begin
    Count := Count + i -(i * 2) + i;

  end;

  Terminate;
end;
//*******************************************************//

end.
  Mit Zitat antworten Zitat
Benutzerbild von gsh
gsh

Registriert seit: 24. Okt 2004
1.542 Beiträge
 
Delphi XE Architect
 
#2

Re: Basic TThread doesn't work

  Alt 1. Feb 2008, 08:01
You do a synchronize(DoIt); this blocks the application.
Leave the synchronize away and do only synchronize when you whant to access to the MainForm
Alex
"Sage nicht alles, was du weißt, aber wisse alles, was du sagst!" Matthias Claudius
"Wer sich über Kritik ärgert, gibt zu, daß er sie verdient hat." Tacitus
  Mit Zitat antworten Zitat
FaNIX

Registriert seit: 8. Okt 2007
36 Beiträge
 
#3

Re: Basic TThread doesn't work

  Alt 1. Feb 2008, 08:06
Zitat von gsh:
You do a synchronize(DoIt); this blocks the application.
Leave the synchronize away and do only synchronize when you whant to access to the MainForm
OK, thanks ,got it to work. I don't exacly understand what you mean by only use synchronize when i want access ti the mainform?
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#4

Re: Basic TThread doesn't work

  Alt 1. Feb 2008, 08:19
When you call Synchronize the related code runs in the main forms context. In your case DoIt doesn't run in the context of the thread but in the context of your main form. You shoukd only call Synchronize if you want to access your main form for updating displayed data.
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
FaNIX

Registriert seit: 8. Okt 2007
36 Beiträge
 
#5

Re: Basic TThread doesn't work

  Alt 1. Feb 2008, 08:26
Zitat von Luckie:
When you call Synchronize the related code runs in the main forms context. In your case DoIt doesn't run in the context of the thread but in the context of your main form. You shoukd only call Synchronize if you want to access your main form for updating displayed data.
Ahh ok, I see what you meen. But how would I update a label on the main form when calling a method inside the thread with Syncronize? Do i need to include the Mainform unit in my TThread and then access the components from there? Something like form1.label.caption := 'Test'. I'm not sure if that is a nice way of doing it, unless there is another way?

Thanks...
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#6

Re: Basic TThread doesn't work

  Alt 1. Feb 2008, 08:50
No, if you want to access controls of your main form you have to use Synchronize because the VCL is not threadsafe. But don't call it everytime in the body of your loop. Call it only evyre 10th time or so.
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
alzaimar
(Moderator)

Registriert seit: 6. Mai 2005
Ort: Berlin
4.956 Beiträge
 
Delphi 2007 Enterprise
 
#7

Re: Basic TThread doesn't work

  Alt 1. Feb 2008, 09:12
This is a bit tricky. Here's a simple example:
Delphi-Quellcode:
Type
  TMythread = Class (TThread)
  private
     fLabelMsg : String; // a private field to display in the main form from within a thread
     procedure DoShowMessage;
  Protected
     Procedure Execute; Override;
  End;
Procedure TMyThread.DoShowMessage;
Begin
  MainForm.MyLabel.Caption := fLabelMsg;
End;

Procedure TMyThread.Execute;
Begin
  ....
  fLabelMsg :='Show this!'; // Write what you want to display in your private fields
  Synchronize (DoShowMessage); // Call the method which actually talks to the VCL via Synchronize
  ...
// The Thread terminates automatically, when the Execute-Method is exited.
End;
Bear in mind that ANY access to VCL-Controls (not only on the main form) must be wrapped in a Synchronize call, otherwise your app will hang.
"Wenn ist das Nunstruck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput!"
(Monty Python "Joke Warefare")
  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 01:29 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