Autor Beitrag
bigkahoona
Hält's aus hier
Beiträge: 2



BeitragVerfasst: So 18.05.08 20:20 
Also ich habe mal ein bisschen mit TThread rumgespielt und dabei das exzellente Tutorial - beziehungsweise ein Codebeispiel - von Michael Puff als Basis benutzt (www.michael-puff.de/...reads_mit_Delphi.pdf).
[EDIT] Es geht hier darum, drei Progressbars auf Form1 durch ein VCL Thread-Objekt (Unit2) über Synchronize aktualisieren zu lassen. Das Programm erfüllt keinerlei Zweck, außer mir das Verständnis des Gebrauchs des Thread-Objekts zu ermöglichen.
Mein Code kompiliert ohne Fehler oder Warnungen. Leider bekomme ich sobald ich das Knöppchen drücke um den Thread zu initialisieren eine EAccessViolation zurück:

Moderiert von user profile iconNarses: Bild als Anhang hochgeladen

Das sind meine Unit1 und Unit2:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ProgressBar1: TProgressBar;
    ProgressBar2: TProgressBar;
    ProgressBar3: TProgressBar;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
    ThatThread: TMyThread;
  public
    procedure updateprogressone(progressonecount: Integer);
    procedure updateprogresstwo(progresstwocount: Integer);
    procedure updateprogressthree(progressthreecount: Integer);
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.updateprogressone(progressonecount: Integer);
begin
  Progressbar1.Position := progressonecount;
end;
procedure TForm1.updateprogresstwo(progresstwocount: Integer);
begin
  Progressbar2.Position := progresstwocount;
end;
procedure TForm1.updateprogressthree(progressthreecount: Integer);
begin
  Progressbar3.Position := progressthreecount;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ThatThread.Create(True);
  ThatThread.FreeOnTerminate := true;
  ThatThread.Resume;
end;

end.

Thread-Objekt Unit2:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
unit Unit2;

interface

uses
  Classes;

type
  TMyThread = class(TThread)
  private
    procedure updateprogressone;
    procedure updateprogresstwo;
    procedure updateprogressthree;
    { Private-Deklarationen }
  protected
    procedure Execute; override;
  public
    gprogressonecount: Integer;
    gprogresstwocount: Integer;
    gprogressthreecount: Integer;
  end;

implementation
uses
  Unit1;

procedure TMyThread.Execute;
var
  loopone, looptwo, loopthree: Integer;
begin
  for loopone := 1 to 100 do
    gprogressthreecount := loopthree;
    Synchronize(updateprogressthree);
    begin
      for looptwo := 1 to 100 do
        begin
          gprogresstwocount := looptwo;
          Synchronize(updateprogresstwo);
          for loopthree := 1 to 100 do
            begin
              gprogressonecount := loopone;
              Synchronize(updateprogressone);
            end;
        end;
    end;
end;

procedure TMyThread.updateprogressone;
begin
  Form1.updateprogressone(gprogressonecount);
end;

procedure TMyThread.updateprogresstwo;
begin
  Form1.updateprogresstwo(gprogresstwocount);
end;

procedure TMyThread.updateprogressthree;
begin
  Form1.updateprogressthree(gprogressthreecount);
end;

end.


Kann mir wer helfen?
Einloggen, um Attachments anzusehen!
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 18.05.08 21:50 
Moin und :welcome: im Forum!

user profile iconbigkahoona hat folgendes geschrieben:
Leider bekomme ich sobald ich das Knöppchen drücke um den Thread zu initialisieren eine EAccessViolation zurück:
Klar, du erzeugst das Thread-Objekt ja auch nicht richtig. :? Probier das mal so: ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ThatThread := TMyThread.Create(True);
  ThatThread.FreeOnTerminate := True;
  ThatThread.Resume;
end;
cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
bigkahoona Threadstarter
Hält's aus hier
Beiträge: 2



BeitragVerfasst: So 18.05.08 22:22 
Vielen tausend Dank! Das war es. :)