tags:

views:

82

answers:

3

hi all

how can i pause and resume in application while working with loops

i can put some sleep(xxx) at begininsg of my loop to pause ,but i want pause when ever i want and resume when i ever i need

any ideas ?

thanks in advance

ok here is alittle more explanation

for i:=0 to 100 do    
begin    
 if button1.clicked then pause //stop the operation and wait for resume button    
 if button2.clicked  then resume  //resume the operations     
end;

edit2 :

ok guys i will tell an example well take any checker for suppose proxy checker ,i have a bunch of proxies loaded in my tlistviwe i am checking them all ,by using lop for i:=0 to listview.items.count do ......

i want to pause my checking operation when ever i want and resume when ever i need

am i clear or still i have to explain some ? :S

regards

A: 

Check if a key is pressed?

Wildhorn
+5  A: 

You need a boolean flag that will indicate whether it's safe to continue looping. If something happens that makes it so you need to pause, it should set the variable to false. This means that, unless you're working with multiple threads, whatever sets this flag will have to be checked inside the loop somewhere. Then at the top (or the bottom) of your loop, check to see if this variable is true and pause otherwise.

Here's the basic idea, in the context of your explanation:

procedure TForm1.DoLoop;
begin
  FCanContinue := true;
  for i:=0 to 100 do
  begin
    //do whatever
    Application.ProcessMessages; //allow the UI to respond to button clicks
    if not FCanContinue then Pause;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   FCanContinue := false;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
   FCanContinue := true;
   Resume;
end;

This is a very simplistic implementation. What you really should do if you have a long-running task that you need to be able to pause and resume on command is put this in a separate thread and take a look at the TEvent class in SyncObjs.

Mason Wheeler
Hey, you shouldn't deploy the whole application! ;-)
splash
+1 for threads, it's the only real solution.
Cosmin Prund
thanks for explanation sir and about threads is there any example ?
steve0
@Steve0: Yeah, there are examples all over. Try googling for "delphi threading tutorial" and see what comes up.
Mason Wheeler
+1  A: 

OK, I don't understand what you are trying to do, but here is some pseudo code:

for i:=0 to 100 do 
begin
  if button1.clicked then
  begin
    while not button2.clicked do
      sleep(50);
  end;
end;
splash
well i want to pause the operations sir but not for pecified time i want to resume when ever i need ,anyway thanks for reply sir
steve0