Autor Beitrag
GericasS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Mo 03.12.07 14:28 
Hey Jungs,

ich hab eben mal die Sleep Funktion von Delphi ausprobiert und hab als Beispiel folgenden Code.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.Add('exit') ;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if
    ComboBox1.Text = 'exit' then
    sleep(2000) ;
    ListBox1.Items.Add('running...');
    sleep(1000) ;
    ListBox1.Items.Add('done') ;
    Form1.Close ;
end;

end.


Folgendes Problem : Wenn ich auf Button1 klicke, dauert es 2 sekunden dann erscheint nicht und das Programm schließt sich einfach. Eigentlich sollte, wenn "exit" ausgewählt ist, nach der sleep time erst running und anschließen done erscheinen. Tut es aber nicht woran liegt der Fehler ?

Grüße

GericasS

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
PeterPain
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 83



BeitragVerfasst: Mo 03.12.07 14:35 
Das Problem liegt daran, dass dein programm während es "schläft" keine messages mehr verarbeitet, ergo wird die neue Caption garnicht gezeichnet. Füg mal vor dem Sleep jeweils ein Application.processmessages; ein, dann sollte es gehen.


gruss
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Mo 03.12.07 14:38 
Hey Peterpain,

danke das wusste ich noch nicht funktioniert super !

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.Add('exit') ;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if
    ComboBox1.Text = 'exit' then
    Application.processmessages;
    sleep(2000) ;
    ListBox1.Items.Add('running...');
    Application.processmessages;
    sleep(1000) ;
    ListBox1.Items.Add('done') ;
    Application.processmessages;
    sleep(1000);
    Form1.Close ;
end;

end.


so Frage beantwortet !

grüße

GericasS

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
PeterPain
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 83



BeitragVerfasst: Mo 03.12.07 15:14 
Alternativ kann ich dir auch diese Delay prozedur von Swissdelphicenter nahelegen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
procedure Delay(Milliseconds: Integer);
  {by Hagen Reddmann}
var
  Tick: DWORD;
  Event: THandle;
begin
  Event := CreateEvent(nil, False, False, nil);
  try
    Tick := GetTickCount + DWORD(Milliseconds);
    while (Milliseconds > 0) and
      (MsgWaitForMultipleObjects(1, Event, False, Milliseconds,
      QS_ALLINPUT) <> WAIT_TIMEOUT) do
    begin
      Application.ProcessMessages;
      Milliseconds := Tick - GetTickCount;
    end;
  finally
    CloseHandle(Event);
  end;
end;


Damit reagiert dein Programm auch noch ;)