Autor Beitrag
winx
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 249



BeitragVerfasst: Sa 27.08.05 12:28 
Hallo,

nachdem ich es normal nicht geschafft habe, will ich nun versuchen, während meiner WS-Anfrage, ein InfoForm (in diesem MUSS sich was bewegen) anzuzeigen.
Habe mir gedacht, dass das nur noch mit Threads funktionieren kann...

Mein Thread sieht so aus:
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:
type
  TInformationThread = class(TThread)
  private
    m_infoForm : TFormInformation;
    i:integer;
    procedure UpdateInformation();
  protected
    procedure Execute; override;
    procedure showInformationForm();
  public
    constructor create(form:TFormInformation);
  end;

implementation

{ InformationThread }

constructor TInformationThread.create(form: TFormInformation);
begin
  inherited create(true);
  m_infoForm := form;
end;


procedure TInformationThread.Execute;
begin
  i:=0;
  showInformationForm();

end;


procedure TInformationThread.showInformationForm;
begin

  while true do begin

    if self.Terminated then begin
      exit;
    end;

    Synchronize(UpdateInformation);

    inc(i);
  end;

end;


procedure TInformationThread.UpdateInformation;
begin

 // m_infoForm.Show;
 // m_infoForm.BringToFront;
  m_infoForm.setInfoText('Update '+IntTostr(i));
  m_infoForm.Update;
  Application.ProcessMessages;
  
end;
end.


Dieser wird folgendermaßen aufgerufen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
infoForm := TFormInformation.Create(nil);
infoForm.Show;
informationThread := TInformationThread.create(infoForm);
informationThread.FreeOnTerminate := true;
informationThread.Resume;
//Hier kommt die sehr lange andauernde Webservice Anfrage
informationThread.Terminate;
if assigned(infoform) then begin
      infoForm.Close;
      infoForm.Free;
end;


Das form geht zwar auf, aber es ändert sich leider kein Text. Die Methode die im Form aufgerufen wird lautet:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TFormInformation.setInfoText(text: string);
begin
  PanelInformation.Caption := text;
  self.Update;
  Application.ProcessMessages;
end;


Könnt ihr mir helfen und Tipps geben oder ein funktionierende Bsp posten?
Hab schon im Forum gesucht und auch das Tutorial angesehen, aber ich finde meinen Fehler einfach nicht...

Danke,
winx

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Sa 27.08.05 13:47 
Welche Delphiversion? Bei D6 bis SP2 (oder 1?) gabs mit dem Synchronize Probleme...
winx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 249



BeitragVerfasst: Sa 27.08.05 14:01 
habe Delphi 7 Enterprise Version, dazu WinXP mit SP2
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: So 28.08.05 07:57 
So wie es aussieht, dürfte der Hauptthread mit der Webanfrage beschäftigt sein. Versuch es doch umgekehrt, pack also die Anfrage in einen Thread und warte in der Hauptanwendung, bis der Thread terminiert.
winx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 249



BeitragVerfasst: Mo 29.08.05 07:21 
guten morgen.

das habe ich auch schon versucht. allerdings scheint dann die web anfrage nichts mehr zu machen. ist echt verzwickt das ganze...
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Mo 29.08.05 20:40 
Wie funktioniert denn die Web-Abfrage? Benutzt Sie COM? Dann benötigst Du im Thread ein CoInitialize oder CoInitializeEx.
winx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 249



BeitragVerfasst: Di 30.08.05 06:51 
Genau so eine Exception mit CoInitialize bekomme ich....was muß ich denn da machen?
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Di 30.08.05 07:47 
Erstmal klären, WIE die Web-Anfrage weggeschickt wird.
Wenn es über ein COM-Objekt geht, dann musst Du im TMyThread.Create das CoInitialize aufrufen (check mal die OH). Alternativ kannst du auch versuchen, im Kontext des Threads (also im Create) die Verbindung aufzubauen bzw. die Web-Komponente zu instantiieren.
winx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 249



BeitragVerfasst: Di 30.08.05 08:19 
Hab jetzt folgendes gemacht und ich bekomme auch die Thread Anfrage an den Webservice:

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:
{ ThreadGetMaterial }

constructor TThreadGetMaterial.create(id,matnr,test:string);
begin

  inherited create(false);
  self.Priority := tpNormal;
  FreeOnTerminate := false;
  m_response := '';

end;

procedure TThreadGetMaterial.Execute;
begin

  Synchronize(getData);

end;


procedure TThreadGetMaterial.getData;
begin
  try
    m_response := //WebService aufruf /// er klappt :-)
  except
    on E: Exception do m_response := e.Message;
  end;
end;

initialization
  CoInitialize(nil);


finalization
  CoUnInitialize();

end.


Der Aufruf sieht dann so aus:

Doch leider Updated sich entweder mein Fenster (wenn ich Application.Process Messages) rein nehme und der Thread
macht nix, oder umgekehrt...ich will aber beides :-)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
  infoForm := TFormInformation.Create(nil);
  infoForm.setInfoText('Receiving Data');
  infoForm.Show();

  try

    materialThread := TThreadGetMaterial.create(...)
    startTime := GetTickCount();

    while materialThread.Response = '' do begin

      infoform.setInfoText(IntToStr(Gettickcount));
      infoForm.Update;
      //Application.ProcessMessages;

    end;
    ...


Blick jetzt gar nix mehr :-(

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Di 30.08.05 09:22 
Erstmal würde ich so nicht darauf warten, das die Web-Anfrage endlich mal fertich ist.
Ist die Response-Eigenschaft des Threads denn überhaupt durch eine CriticalSection geschützt?

Ich würde es so machen:
1. Thread erzeugen (suspended)
2. Thread.OnTerminate auf eine Methode umbiegen, die das Infofenster wieder schließt.
3. Infofenster anzeigen
4. Thread aufwecken (mit resume)
... ab hier kann Windows dann wieder übernehmen

Das infofenster 'updated' dann alle paar 100 Millisekunden einen Zähler, oder zeigt einen Film, oder irgendwas.

Auf diese Weise läuft deine Anwendung fein im Hauptthread weiter, die Applikation kann auf Windows-Events reagieren und überhaupt ist das sowieso hübscher, findste nicht?

Probiers mal aus.
winx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 249



BeitragVerfasst: Di 30.08.05 10:05 
danke,

noch zwei Fragen dazu:

1. wie und wo soll ich Thread.OnTerminate auf eine Methode umbiegen die das fesnter schließt?
2. würdest du im infofenster also praktisch nen timer laufen lassen?

Danke,
winx