Hallo!
Wie im Titel angekündigt benutze ich Threads um einige Dateien runterzuladen. Allerdings sieht mir das ganze nicht besonders schön aus. Vielleicht weiss einer wie es effektiver geht ohne es zu kompliziert zu machen? (Um es nochmal zu verdeutlichen: Der Code läuft fehlerfrei!)
Was will ich programmieren?
Die Dateiliste befindet sich in einem dynamischen array 'AllData'. Nun will alle Dateien aus der Liste runterladen indem ich 10 Threads benutze. Der gesamte Download wird in einer Funktion 'Update1' ausgeführt. Der Fortschritt wird in Form_Progress.Listbox_Log sichtbar gemacht.
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:
| unit unit1;
...
type TDownload = class(TThread) public Index: integer; Nbr: integer; Form_Progress: TForm_Progress; protected procedure Execute; override; end;
...
function Update1(Index: integer): integer; implementation
...
procedure TDownload.Execute; begin try Nbr := Update1(AllData[Index]); Form_Progress.ListBox_Log.Items.Add(AllData[Index]+' - '+IntToStr(Nbr)); Form_Progress.ListBox_Log.TopIndex := Form_Progress.ListBox_Log.Count-1; except Form_Progress.ListBox_Log.Items.Add(AllData[Index]+' - Error occured'); Form_Progress.ListBox_Log.TopIndex := Form_Progress.ListBox_Log.Count-1; end; Terminate; end;
procedure TForm_Main.Button_UpdateClick(Sender: TObject); var i, j: integer; Nbr: integer; Form_Progress: TForm_Progress; DownloadThreads: array [0..9] of TDownload; begin
Form_Progress := TForm_Progress.Create(Self); Form_Progress.Show; Form_Progress.Borderstyle := bsSingle; StopDownloading := False; for j := 0 to 9 do begin DownloadThreads[j] := TDownload.Create(True); DownloadThreads[j].FreeOnTerminate := False; DownloadThreads[j].Index := j; DownloadThreads[j].Form_Progress := Form_Progress; DownloadThreads[j].Resume; end;
i := 10; repeat for j := 0 to 9 do begin if DownloadThreads[j].Terminated then begin DownloadThreads[j].Free; DownloadThreads[j] := TDownload.Create(True); DownloadThreads[j].FreeOnTerminate := False; DownloadThreads[j].Index := i; DownloadThreads[j].Form_Progress := Form_Progress; DownloadThreads[j].Resume; Inc(i); Form_Progress.Label_Nbr.Caption := IntToSTr(i-9)+'/'+IntToStr(Length(AllData)); Form_Progress.Refresh; Application.ProcessMessages; break; end; end; if StopDownloading = True then break; Delay(200); until i >= Length(AllData); j := 0; repeat if DownloadThreads[j].Terminated then begin DownloadThreads[j].Free; Inc(j); Inc(i); Form_Progress.Label_Nbr.Caption := IntToSTr(i-9)+'/'+IntToStr(Length(AllData)); Form_Progress.Refresh; end; Delay(50); until j > 9; end;
... |