Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Progressbar um Loadfromfile fortschritt anzuzeigen. (https://www.delphipraxis.net/144901-progressbar-um-loadfromfile-fortschritt-anzuzeigen.html)

himitsu 19. Dez 2009 13:07

Re: Progressbar um Loadfromfile fortschritt anzuzeigen.
 
OK, da ist der Integer wohl etwas zu klein :lol:

ersetze mal diese Zeilen (3 mal)
Delphi-Quellcode:
If Assigned(FProgress) Then FProgress(Self, Length(Value)
  * 9999 div Integer(P - Integer(Value)));
durch diese
Delphi-Quellcode:
If Assigned(FProgress) Then FProgress(Self, Int64(Length(Value))
  * 9999 div ((Integer(P) - Integer(Value)) div SizeOf(Char)));
wobei es so auch gehn sollte
Delphi-Quellcode:
If Assigned(FProgress) Then FProgress(Self, Trunc(Length(Value)
  * 9999.0 / ((Integer(P) - Integer(Value)) div SizeOf(Char))));
bzw. ich hab es in Beitrag #7 schon ersetzt

(hoffentlich stimmt nun auch die Klammersetzung)

Nelphin 19. Dez 2009 13:40

Re: Progressbar um Loadfromfile fortschritt anzuzeigen.
 
Zitat:

Zitat von himitsu
OK, da ist der Integer wohl etwas zu klein :lol:

ersetze mal diese Zeilen (3 mal)
Delphi-Quellcode:
If Assigned(FProgress) Then FProgress(Self, Length(Value)
  * 9999 div Integer(P - Integer(Value)));
durch diese
Delphi-Quellcode:
If Assigned(FProgress) Then FProgress(Self, Int64(Length(Value))
  * 9999 div ((Integer(P) - Integer(Value)) div SizeOf(Char)));
wobei es so auch gehn sollte
Delphi-Quellcode:
If Assigned(FProgress) Then FProgress(Self, Trunc(Length(Value)
  * 9999.0 / ((Integer(P) - Integer(Value)) div SizeOf(Char))));
bzw. ich hab es in Beitrag #7 schon ersetzt

(hoffentlich stimmt nun auch die Klammersetzung)

habe die zeilen ausgetauscht jetzt kommt beim laden des selben datensatzes folgendes von progress zurück:

0
-1874890282
1463535298
1214422907
746116034
538470534

am ende kommt dann ganz oft 10001 und schliesslich 9999.

himitsu 19. Dez 2009 14:56

Re: Progressbar um Loadfromfile fortschritt anzuzeigen.
 
Mathe 5, würd ich mal sagen :roll:

nja, immerhin ist der Code jetzt kürzer
Delphi-Quellcode:
// If Progress is -3, then starting to read the file or stream.
// If Progress is -2, then starting to decode. (only in Delphi 2009 and successors)
// If Progress is -1, then started to empty the old list.
// If Progress is 0, then starting the Add.
// If Progress is between 0 and 10000, then the text will be added to the list
//   and "Progress" is the progress in hundredths of a percent.
// If Progress is 10000, then the read is completed.

Type TProgressEvent = Procedure(Sender: TObject; Progress: Integer) of Object;
  TProgressStringList = Class(TStringList)
  Private
    FProgress: TProgressEvent;
  Protected
    Procedure SetTextStr(Const Value: String); Override;
    Property OnProgress: TProgressEvent Read FProgress Write FProgress;
  Public
    {$IFDEF UNICODE}
      Procedure LoadFromStream(Stream: TStream; Encoding: TEncoding); Override;
    {$ELSE}
      Procedure LoadFromStream(Stream: TStream); Override;
    {$ENDIF}
  End;

Procedure TProgressStringList.SetTextStr(Const Value: String);
  Var P, Start: PChar;
    S: String;
    {$IFDEF UNICODE}
      LB: PChar;
      LineBreakLen: Integer;
    {$ENDIF}
    C: LongWord;

  Begin
    BeginUpdate;
    Try
      If Assigned(FProgress) Then FProgress(Self, -1);
      Clear;
      If Assigned(FProgress) Then FProgress(Self, 0);
      C := GetTickCount;
      P := Pointer(Value);
      If P <> nil Then
        {$IFDEF UNICODE}
        If CompareStr(LineBreak, sLineBreak) = 0 Then Begin
        {$ENDIF}
          // This is a lot faster than using StrPos/AnsiStrPos when
          // LineBreak is the default (#13#10)
          While P^ <> #0 do Begin
            Start := P;
            While not (P^ in [#0, #10, #13]) do Inc(P);
            SetString(S, Start, P - Start);
            Add(S);
            If P^ = #13 Then Inc(P);
            If P^ = #10 Then Inc(P);
            If Assigned(FProgress) and (GetTickCount - C > 50) Then Begin
              FProgress(Self, Int64((Integer(P) - Integer(Value))
                div SizeOf(Char)) * 9999 div Length(Value));
              C := GetTickCount;
            End;
          End;
        {$IFDEF UNICODE}
        End Else Begin
          LineBreakLen := Length(LineBreak);
          While P^ <> #0 do Begin
            Start := P;
            LB := AnsiStrPos(P, PChar(LineBreak));
            While (P^ <> #0) and (P <> LB) do Inc(P);
            SetString(S, Start, P - Start);
            Add(S);
            If P = LB Then Inc(P, LineBreakLen);
            If Assigned(FProgress) and (GetTickCount - C > 50) Then Begin
              FProgress(Self, Int64((Integer(P) - Integer(Value))
                div SizeOf(Char)) * 9999 div Length(Value));
              C := GetTickCount;
            End;
          End;
        End;
        {$ENDIF}
      If Assigned(FProgress) Then FProgress(Self, 10000);
    Finally
      EndUpdate;
    End;
  End;

Procedure TProgressStringList.LoadFromStream(Stream: TStream
    {$IFDEF UNICODE}; Encoding: TEncoding{$ENDIF} );

  Var Size: Integer;
    S: String;
    {$IFDEF UNICODE} Buffer: TBytes; {$ENDIF}

  Begin
    BeginUpdate;
    Try
      Size := Stream.Size - Stream.Position;
      If Assigned(FProgress) Then FProgress(Self, -3);
      {$IFDEF UNICODE}
        SetLength(Buffer, Size);
        Stream.Read(Buffer[0], Size);
        If Assigned(FProgress) Then FProgress(Self, -2);
        Size := TEncoding.GetBufferEncoding(Buffer, Encoding);
        S := Encoding.GetString(Buffer, Size, Length(Buffer) - Size);
      {$ELSE}
        SetString(S, nil, Size);
        Stream.Read(Pointer(S)^, Size);
      {$ENDIF}
      SetTextStr(S);
    Finally
      EndUpdate;
    End;
  End;
Delphi-Quellcode:
procedure TForm1.ProgressEvent(Sender: TObject; Progress: Integer);
begin
  case Progress of
    -3:   Label1.Caption := 'lese Datei ein ...';
    -2:   Label1.Caption := 'dekodiere den Text ...';
    -1:   Label1.Caption := 'leere alte Liste ...';
    10000: Label1.Caption := 'fertig';
    else begin
      Label1.Caption := 'befülle die Liste';
      ProgressBar1.Position := Progress div 100; // .Min=0 und .Max=100
    end;
  end;
  //Application.ProcessMessages;
  Label1.Refresh;
  ProgressBar1.Refresh;
end;

procedure TForm1.Button1Click(Sender: TObject);
var laden: TOpenDialog;
  start, dauer: Cardinal;
  sl: TProgressStringList;
begin
  laden := TOpenDialog.Create(self);
  try
    if laden.Execute then begin
      sl := TProgressStringList.Create;
      try
        start := GetTickCount();
        sl.OnProgress := ProgressEvent;
        sl.LoadFromFile(laden.FileName);
        dauer := GetTickCount() - start;
        Label1.Caption := 'Laden hat ' + (floattostr(dauer/1000)) + ' Sekunden gedauert';
        //...
      finally
        sl.Free;
      end;
    end;
  finally
    laden.Free;
  end;
end;

Nelphin 19. Dez 2009 18:46

Re: Progressbar um Loadfromfile fortschritt anzuzeigen.
 
Ich verneige mich und sage vielen Dank!!!! :-D :-D
funzt, sieht toll aus und dauert jetzt kaum mehr länger als der original Ladevorgang... das werde ich verwenden!

:thumb: :dp: :thumb:


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:47 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz