Hi,
ich hab ein Problem mit dem Beenden eines bzw. mehrerer Threads beim Schließen des Programms.
Meine Threads erhalten eine IdHTTP Komponente, und laden dann Dateien aus dem Netz herunter. Sobald der Download abgeschlossen ist, wird der Thread terminated. Nun habe ich folgendes Problem:
- Beende ich das Programm während Downloads stattfinden, bekomme ich eine Zugriffsverletzung. Leider sehe ich nicht, wo diese ausgerufen wird.
- Beende ich das Programm nachdem alle Downloads abgeschlossen sind und die zugehörigen Threads terminiert sind, dann bekomme ich eine Zugriffsverletzung beim Closen des Programms, da ich dort alle Threads nochmal terminiere. Leider habe ich keine Möglichkeit gefunden, wie ich überprüfen kann, ob der Thread schon terminiert ist.
Hier mal mein Code:
Threadklasse:
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: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108:
| unit Unit2;
interface
uses Classes, sysutils, StdCtrls, SyncObjs, IdHTTP, IdComponent;
type TDLThread = class(TThread) FCriticalSection: TCriticalSection;
private DLHTTP: TIdHTTP; DLURL, DLDir: String; BytesKopiert, BytesInsgesamt: int64; lStream: TFileStream; DLIndex: integer; GUI: TLabel; protected procedure Execute; override; procedure DoSync; function GetSizeName(const Size : int64): String; public constructor Create(pIndex: integer; pGUI: TLabel; pDLHTTP: TidHTTP; pDLURL, pDLDir: string; CriticalSection: TCriticalSection); function getBK(): int64; function getBG(): int64; function getDLIndex(): integer; procedure setBK(pBK: int64); procedure HTTPWork(); end;
implementation
function TDLThread.GetSizeName(const Size : int64): String; begin Result := floattostr((round((Size/1048576)*100))/100)+' MB'; end;
procedure TDLThread.HTTPWork(); begin Synchronize(DoSync); end;
procedure TDLThread.setBK(pBK: int64); begin BytesKopiert := pBK; end;
function TDLThread.getBG(): int64; begin result := BytesInsgesamt; end;
function TDLThread.getDLIndex(): integer; begin result := DLIndex; end;
function TDLThread.getBK(): int64; begin result := BytesKopiert; end;
constructor TDLThread.Create(pIndex: integer; pGUI: TLabel; pDLHTTP: TidHTTP; pDLURL, pDLDir: string; CriticalSection: TCriticalSection); begin GUI := pGUI; DLIndex := pIndex; DLHTTP := pDLHTTP; DLURL := pDLURL; DLDir := pDLDir; FCriticalSection := CriticalSection; FreeOnTerminate := true; inherited Create(true); end;
procedure TDLThread.Execute; begin try BytesKopiert := 0;
DLHTTP.Head(DLURL); BytesInsgesamt := DLHTTP.Response.ContentLength;
lStream := TFileStream.Create(DLDir, fmCreate or fmShareDenyNone); lStream.Position := 0;
DLHTTP.Request.ContentRangeStart := 0; DLHTTP.Request.ContentRangeEnd := BytesInsgesamt; DLHTTP.Get(DLURL, lStream);
finally if Assigned(lStream) then lStream.Free; end;
Terminate(); end;
procedure TDLThread.DoSync(); begin FCriticalSection.Enter; GUI.Caption := GetSizeName(BytesKopiert)+'/'+GetSizeName(BytesInsgesamt); FCriticalSection.Leave; end;
end. |
Und der Programmcode:
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: 64: 65: 66: 67:
| var dlthreads : array of tdlthread; DLHTTPs: array of TIDHTTP; CS: TCriticalSection;
procedure tform1.dlhttpwork(sender: tobject; AWorkMode: TWorkMode; const AWorkCount: Integer); var dlindex: integer; begin if sender is TIdHTTP then dlindex := (TIdHTTP(sender).Tag);
dlthreads[dlindex].setBK(AWorkCount); dlthreads[dlindex].HTTPWork; end;
procedure TForm1.FormCreate(Sender: TObject); var i: integer; begin CS := TCriticalSection.Create;
setlength(DLHTTPs, 2); for i := 0 to high(DLHTTPS) do begin DLHTTPs[i] := TIDHTTP.Create(self); DLHTTPs[i].Tag := i; DLHTTPs[i].onWork := dlHTTPWork; end;
setlength(dlthreads, 2); dlthreads[0] := tdlthread.Create(0, label1, DLHTTPs[0], 'http://www.blubb.de/test.exe', 'C:\test.exe', CS); dlthreads[1] := tdlthread.Create(1, label2, DLHTTPs[1], 'http://www.blubb.de/test2.exe', 'C:\test2.exe', CS);
dlthreads[0].OnTerminate := ThreadDone; dlthreads[1].OnTerminate := ThreadDone; end;
procedure TForm1.ThreadDone(Sender: TObject); begin if sender is TDLThread then begin
DLHTTPs[(TDLThread(sender).getDLIndex)].Disconnect;
end; end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); var i: integer; begin
for i := 0 to high(dlthreads) do begin DLHTTPs[i].Disconnect; dlthreads[i].Terminate(); end;
CS.Free; end; |
Ich habe es auch schon mit
Delphi-Quelltext
1: 2: 3:
| .Terminate .WaitFor .Free |
versucht, aber das klappt leider genauso wenig..
Ich hoffe, Ihr könnt mit helfen
Danke + Grüße
Passi