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
Narses: Bild als Anhang hochgeladen
Das sind meine Unit1 und Unit2:
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 ThatThread: TMyThread; public procedure updateprogressone(progressonecount: Integer); procedure updateprogresstwo(progresstwocount: Integer); procedure updateprogressthree(progressthreecount: Integer); 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:
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; 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?