Autor Beitrag
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 06.01.10 20:02 
user profile iconbaka0815 hat folgendes geschrieben Zum zitierten Posting springen:
Ist ja auch Quatsch. Wenn dann musst du schon Synchronize(Form1.WriteGrid(GRID_ACTIO, i+1, 'skipped')); aufrufen und nicht TForm!
Ist auch Quatsch, wenn auch nicht ganz so doll. :mrgreen:
Egal wie du es so versuchst, das kann nicht gehen...
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 06.01.10 20:09 
馬鹿0815わ馬鹿です。;-)

ausblenden Delphi-Quelltext
1:
Synchronize(DoWriteCell);					


Und dann in DoWriteCell:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TDeineThreadKlasse.DoWriteCell;
begin
    Form1.WriteGrid(GRID_ACTIO, i+1, 'skipped');
end;


Wobei Du i entsprechend über ein privates Property übergeben musst.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Bergmann89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Do 07.01.10 00:01 
Hey,

irgendwie hängt das immer noch an paar Stellen un ich blick langsam nich mehr durch. Also hab ich mir das Backup ohne Threads besorgt und mach das jetz nochma ordentlich. Dazu hab ich mir diese Klasse geschrieben. Bis jetzt läuft sie ganz gut. Sieht vlt irgendjmd noch n Fehler? Nich das ich wieder von vorn anfang -.-
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:
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:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
unit HTTPThread;

interface

uses
  Classes, 
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

type
  //Event wenn Stream vollständig geladen//Event wenn String vollständig geladen
  TReceiveStreamEvent = procedure(Sender: TObject; Stream: TStream) of Object;
  //Event wenn String vollständig geladen
  TReceiveStringEvent = procedure(Sender: TObject; Code: WideString) of Object;

  //mögliche Operationen (nichts, String laden, Stream ladens)
  THTTPOperation = (hoNone, hoGetString, hoGetStream);
  
  //Verwaltet ein TIdHTTP-Object in einem eigenen Thread-Kontext
  THTTPThread = class(TThread)
  private
    //Stream der aus dem Internet geladen wird
    fStream: TMemoryStream;
    //String der aus dem Internet geladen wird
    fCode: WideString;
    //URL von der geladen werden soll
    fURL: String;
    //zur Kommunikation mit dem Internet
    fHTTP: TIdHTTP;
    //Operation die ausgefürht werden soll
    fOperation: THTTPOperation;

    //Events
    fOnReceiveStream: TReceiveStreamEvent;
    fOnReceiveString: TReceiveStringEvent;

    procedure Execute; override;    //Arbeitet die Anfrage ab
    procedure CallOnReceiveStream;  //ruft das OnReceiveStream-Ereignis auf
    procedure CallOnReceiveString;  //ruft das OnReceiveString-Ereignis auf
public
    //Event-Eigenschaften
    property OnReceiveStream: TReceiveStreamEvent read fOnReceiveStream write fOnReceiveStream;
    property OnReceiveString: TReceiveStringEvent read fOnReceiveString write fOnReceiveString;

    //Object-Eigenschaften
    property HTTP: TIdHTTP read fHTTP; 

    function GetStream(URL: String): Boolean;           //fordert den Thread auf einen Stream aus dem Internet zu laden
    function GetString(URL: String): Boolean;           //fordert den Thread auf einen String aus dem Internet zu laden
    constructor Create(CreateSuspended: Boolean = True);//erzeugt das Object und reserviert Speicher
    destructor Destroy; override;                       //gibt das Object und Speicher wieder frei
  end;


implementation

procedure THTTPThread.Execute;
begin
  case fOperation of                        //der Operation entsprechend entscheiden
    hoGetStream: begin                      //wenn GetStream-Operation
      fHTTP.Get(fURL, fStream);             //Stream aus dem Internet laden
      fStream.Position := 0;                //Stream-Position zurücksetzen
      Synchronize(CallOnReceiveStream);     //Ereignis im Main-Thread ausführen
    end;
    hoGetString: begin                      //wenn GetString-Operation
      fCode := fHTTP.Get(fURL);             //String aus dem Internet laden
      Synchronize(CallOnReceiveString);     //Ereignis im Main-Thread ausführen
    end;
  end;
  fOperation := hoNone;                     //Operation zurücksetzen
end;

procedure THTTPThread.CallOnReceiveStream;
begin
  if Assigned(fOnReceiveStream) then
    fOnReceiveStream(self, fStream);   
end;

procedure THTTPThread.CallOnReceiveString;
begin
  if Assigned(fOnReceiveString) then
    fOnReceiveString(self, fcode);    
end;

//@URL: URL vond er geladen werden soll;
//@result: TRUE, wenn erfolgreich ausgeführt (sonst FALSE);
function THTTPThread.GetStream(URL: String): Boolean;
begin
  if Suspended then begin
    result     := True;
    fURL       := URL;
    fOperation := hoGetStream;
    Resume;
  end else result := false;
end;

//@URL: URL vond er geladen werden soll;
//@result: TRUE, wenn erfolgreich ausgeführt (sonst FALSE);
function THTTPThread.GetString(URL: String): Boolean;
begin
  if Suspended then begin
    result     := True;
    fURL       := URL;
    fOperation := hoGetString;
    Resume;
  end else result := false;
end;

//@CreateSuspended: Soll der Thread pausiert gestartet werden? (ja/nein);
constructor THTTPThread.Create(CreateSuspended: Boolean);
begin
  inherited Create(CreateSuspended);

  fStream := TMemoryStream.Create;
  fHTTP   := TIdHTTP.Create(nil);

  fOperation := hoNone;
  fURL       := ''; 
end;

destructor THTTPThread.Destroy;
begin
  fStream.Free;
  fHTTP.Free;
  inherited Destroy;
end;

end.

MfG Bergmann

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
Bergmann89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Di 12.01.10 02:19 
Hey,

ich ma wieder. Und immer noch das selbe Problem. Ich hab das Projekt jetz nochma neu aufgesetzt (paar Code stellen auch kopiert) und es bleibt wieder stecken.
Ich hab auch rausgefunden wo in etwa, aber ich kann mir einfach keinen Reim darauf machen. Hier ma die Stelle:

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:
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:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
//[...]
  //THTTPThread-Code entspricht dem aus dem vorhergehenden Post
  HTTP := THTTPThread.Create(True);
  HTTP.HTTP.HandleRedirects := True;
  HTTP.OnReceiveString      := HTTPReceiveCode;
  HTTP.OnError              := HTTPError;
  HTTP.FreeOnTerminate      := True;
  HTTP.GetString(GetURL);
//[...]

procedure TInterpret.HTTPReceiveCode(Sender: TObject; Code: WideString);
var Stack: TStringList;
var j: Integer;
var CopyLink, CopyName: Boolean;
var TmpAlbum: TAlbum;
var TmpTitel: TTitel; 

const ALBUM_SEARCH = '<ul class=''albums''>';
const SONGS_SEARCH = '<ul class=''songs''>';

  procedure FormatName(var tmp: TAlbum);
  var i, p1: Integer;
  var CopyName: Boolean;
  var str: String;
  begin
    str := tmp.fName;
    CopyName := True;
    for i := 1 to Length(str) do begin
      if str[i] = '_' then str[i] := ' ';
    end;
    i := Length(str);
    while (str[i] <> ')') and (i <= Length(str)) do dec(i);
    p1 := i;
    tmp.fYear := '';
    while (str[i] <> '(') and (i <= Length(str)) do begin
      tmp.fYear := str[i] + tmp.fYear;
      dec(i);
    end;
    delete(str, i, p1-i+1);
    while str[Length(str)] = ' ' do
      delete(str, Length(str), 1);
    tmp.fName := str;
  end;

begin
  Stack := TStringList.Create;
  TmpAlbum := nil;

  writeln('receive code: '+fName);

  j := 1;
  while j <= Length(Code) do begin
    if copy(code, j, Length(ALBUM_SEARCH)) = ALBUM_SEARCH then
      Stack.Add('album');
    if copy(code, j, Length(SONGS_SEARCH)) = SONGS_SEARCH then
      Stack.Add('songs');
    if copy(code, j, 5) = '</ul>' then
      Stack.Delete(Stack.Count-1);

    if copy(code, j, 4) = '<li>' then begin
      //ALBEN BESORGEN////////////////////////////////////////////////////////////////////////////////////////////////////////////
      if Stack[Stack.Count-1] = 'album' then begin
        CopyLink := false;
        CopyName := false;
        TmpAlbum := TAlbum.Create;
        fAlbumList.Add(TmpAlbum);
        //ALBEN LINK BESORGEN////////////////////////////////////////////////////////////////////////////////////////////////////////////
        while j < Length(code) do begin
          if copy(code, j, 6) = 'href=''' then begin
            inc(j, 6);
            CopyLink := True;
          end;
          if CopyLink and (Code[j] = '''') then begin
            break;
          end;
          if CopyLink then TmpAlbum.fLink := TmpAlbum.fLink + code[j];
          inc(j);
        end;
        //ALBEN NAME BESORGEN////////////////////////////////////////////////////////////////////////////////////////////////////////////
        while j <= Length(Code) do begin
          if Code[j] = '>' then begin
            inc(j);
            CopyName := True;
          end;
          if Code[j] = '<' then begin
            break;
          end;
          if CopyName then TmpAlbum.fName := TmpAlbum.fName + Code[j];
          inc(j);
        end;
        FormatName(TmpAlbum);
      end;

      //SONGS BESORGEN////////////////////////////////////////////////////////////////////////////////////////////////////////////
      if Stack[Stack.Count-1] = 'songs' then begin
        CopyLink := false;
        CopyName := false;
        TmpTitel := TTitel.Create;
        TmpAlbum.fTitelList.Add(TmpTitel);
        //SONG LINK BESORGEN////////////////////////////////////////////////////////////////////////////////////////////////////////////
        while j < Length(code) do begin
          if copy(code, j, 6) = 'href=''' then begin
            inc(j, 6);
            CopyLink := True;
          end;
          if CopyLink and (Code[j] = '''') then begin
            break;
          end;
          if CopyLink then TmpTitel.fLink := TmpTitel.fLink + code[j];
          inc(j);
        end;
        //SONG NAME BESORGEN////////////////////////////////////////////////////////////////////////////////////////////////////////////
        while j <= Length(Code) do begin
          if Code[j] = '>' then begin
            inc(j);
            CopyName := True;
          end;
          if Code[j] = '<' then begin
            break;
          end;
          if CopyName then TmpTitel.fName := TmpTitel.fName + Code[j];
          inc(j);
        end;
      end;
    end;
    inc(j);
  end; 

  if Assigned(fOnFinishLoad) then
    fOnFinishLoad(self, true);

  Stack.Free;
end;

Normalerweiße sollte er ja die Porzedur auf im Kontext des MainThread ausführen, aber warum bricht der dann einfach ab?! Ich bin Ratlos :?!?:

MfG Bergmann.

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Di 12.01.10 08:54 
Was passiert bei try...except ?

_________________
PROGRAMMER: A device for converting coffee into software.
Bergmann89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Di 12.01.10 16:26 
Hey,

ausblenden 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:
procedure THTTPThread.Execute;
begin
  case fOperation of                        //der Operation entsprechend entscheiden
    hoGetStream: begin                      //wenn GetStream-Operation
      try
        fHTTP.Get(fURL, fStream);           //Stream aus dem Internet laden
      except
        fLastErrorCode := THREAD_HTTP_ERROR;
        Synchronize(CallOnError);
        exit;
      end;
      fStream.Position := 0;                //Stream-Position zurücksetzen
      Synchronize(CallOnReceiveStream);     //Ereignis im Main-Thread ausführen
    end;
    hoGetString: begin                      //wenn GetString-Operation
      try
        fCode := fHTTP.Get(fURL);           //String aus dem Internet laden
      except
        fLastErrorCode := THREAD_HTTP_ERROR;
        Synchronize(CallOnError);
        exit;
      end;
      Synchronize(CallOnReceiveString);     //Ereignis im Main-Thread ausführen
    end;
  end;
  fOperation := hoNone;                     //Operation zurücksetzen
end;

da bringt jetzt der Compiler ne Meldung (wenn aktiviert) wenn das HTTP-Objekt ne Exception wirft, aber das Programm arbeitet normal weiter. Bricht aber trotzdem noch irgendwann vorzeitig ab.

€: wenn ich um die ganze HTTPReceiveCode-Prozedur n try-except mach, dann kommt ne Exception, aber ich weiß nich wie ich mir den Exception-Text ausgeben lassen kann.

MfG Bergmann

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 12.01.10 16:43 
Moin!

user profile iconBergmann89 hat folgendes geschrieben Zum zitierten Posting springen:
aber ich weiß nich wie ich mir den Exception-Text ausgeben lassen kann.
z.B. so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
try
  // crash
except
  ShowMessage(Exception(ExceptObject).Message);
end;
cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Di 12.01.10 17:04 
Hatten wir doch schon, dass das in nem Thread nich so toll ist ;)
Versuchen kann mans, ja.
Ansonsten
- OutputDebugString
- Haltepunkt setzen und E.Message auswerten
- Zur Not mit nem FileStream in eine Datei schreiben.

_________________
PROGRAMMER: A device for converting coffee into software.
Bergmann89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Di 12.01.10 17:12 
Hey,

reintheoretisch is es ja nich mehr in dem Thread, sondern in dem Event, was durch Synchronize von dem Thread aufgerufen wird. Ich guck gleich ma nach was er sagt...

€: Is ne Zugriffsverletzung beim Lesen. Hm, dann is bestimmt irgendwo der Index von dem String zu hoch. Ich überarbeite die Methode nochma. Ich glaub das liegt an den 2 verschalchtelten Schleifen die beide mit j arbeiten...

MfG Bergmann

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^