Delphi Pause Program
I've got an algorithm. I'd like to pause it at some point and then continue once the user presses a button.
How do I do that? I've browsed the documentation, and searched the internet, but no luck yet.
Delphi Pause
Here's a relevant code snip: if AiAi+1 then begin Zameni(i,i+1); done:=true; sleep(pauza); br:=br+1; end; Right now, I use sleep (pauza is just a constant, means pause in Serbian). Ideally, I'd like to replace that line with a procedure which would sleep for the interval, or wait for a button press based on a configuration setting. EDIT1: Ah yes, if it wasn't obvious - it's a graphics application, not console, so slapping a 'readln' won't work (sadly). I wouldn't recommend it as good application design, but if none of the other suggestions are suitable for you, you may prefer this. Add a 'Paused: Boolean' field to your class/form, and a 'Continue' button. When you start the operation, set Paused to False, and Continue.Enabled:= False; When your code reaches the section where you want to pause: Paused:= True; Continue.Enabled:= True; while Paused do begin sleep(100); Application.ProcessMessages; end; In your Continue buttons event handler: procedure Form1.ContinueClick(Sender: TObject); begin Paused:= False; Continue.Enabled:= False; end; As I said before, not pretty, but it should get the job done. Use a thread and the built-in suspend/resume functions for the TThread class.
See More On Stackoverflow
Jens Borrisholt wrote a nice little example article on about.com doing this kind of thing. If you introduce multiple ProcessMessage locations in the application, you can have problems with code execution from other events being left running when the form closes. For instance, if the form was closed while in the ProcessMessage 'sleep' mode, there is no good way to unwind the stack. MessageDialog may not work if you are using DirectX/OpenGL since you won't have the normal display open. Calling MessageDialog may switch the view from the DirectX view to normal desktop view (ugly but workable) OR it may show the message on the normal desktop view without changing the display mode from DirectX (making it impossible for the user to see the prompt).
How do you get program execution to pause? I am trying to use the FTP Internet component to run the TYPE_ method followed immediately by the GETFILE method.
It's been a long time since I've worked with Dx, but I recall issues about failing to change view modes being a pain. Edit: As Alexander points out, you shouldn't suspend the thread from anywhere but IN the thread (i.e., Thread A can suspend Thread A but not Thread B). If you want the GUI thread to suspend the sorting thread, send the sorting thread a message and handle the suspend inside the sorting thread. His link contains a good discussion on why this is so.