smart-mobile-studio

Using a delay (w3_setTimeOut) with Visual Control Application


Using Smart Mobile Studio 2.2.0.4165 (Beta)

I am not sure if I am doing this right, and It may be that I just don't understand how to use the w3_TimeOut callback properly.

The following code locks up when i click the start button. My start button is stuck in the down position, and the program locks up. When I close the program, I then get an "External Exception". I have to restart SMS to do anything more. What have I done wrong?

procedure TForm1.StartClick(Sender: TObject);
var
 i: integer;
begin
 //initialize variable
 fRunning:= true;
 repeat
  //get a random light (1-4) and add to array
  fLights.Add(RandomInt(4)+1);
  //step through lights array and light up each light
  for i:= 0 to fLights.Count-1 do
  begin
   LightUp(fLights[i]);
   //add a delay after each
    w3_setTimeOut( procedure ()
    begin
     //shut down light
      LightDwn(fLights[i]);
    end, 200);
  end; //for
 until not fRunning;
end;


procedure TForm1.StopClick(Sender: TObject);
begin
 //reset variable
 fRunning:= false;
 //clear all lights in array
 fLights.Clear;
end;
I even tried
w3_callback( procedure ()
    begin
     LightDwn(fLights[i]);
    end, 200);

If I do not use the delay, and instead, kinda delay it with a show message, it works exactly as I need it too (of course, I can't use the show messages though )

procedure TForm1.StartClick(Sender: TObject);
var
 i: integer;
begin
 //initialize variable
 fRunning:= true;
 repeat
  //get a random light (1-4) and add to array
  fLights.Add(RandomInt(4)+1);
  //step through lights array and light up each light
  for i:= 0 to fLights.Count-1 do
  begin

   LightUp(fLights[i]);
   ShowMessage('Up: ' + intToStr(fLights[i]));

   LightDwn(fLights[i]);
   ShowMessage('Down: ' + intToStr(fLights[i]));

  end; //for
 until not fRunning;
end;

A simple test shows me that the the w3_TimeOut is not a blocking routine e.g.

procedure TForm1.W3Button1Click(Sender: TObject);
var
 I: Integer;
begin
 for I:= 1 to 99 do
 begin
  w3_setTimeOut( procedure ()
    begin
     //shut down light
      W3Label1.Caption:= IntToStr(I);
    end, 1000);
 end;

The label does not get updated until after the loop has completed

I even tried refreshing the form and the label

w3_setTimeOut( procedure ()
    begin
     //shut down light
      W3Label1.Caption:= IntToStr(I);
      W3Label1.Invalidate;
      //self.Invalidate;
    end, 1000);

But, I do not think i am even doing that properly

after some searching, I found the TW3EventRepeater in the SmartCL.Time.

However, it seems that it works exactly like both the w3_setTimeOut and w3_callback

How do i do a wait() after some code, then when the wait expires, do some more code. it must be a blocking wait?

thanx

ANSWER:

 fTimer:= TW3Timer.Create;
 fTimer.Delay:= 1000;
 fTimer.OnTime:= HandleTimer;
 fTimer.Enabled:= True;

Solution

  • JavaScript is single-threaded, therefore timers are only triggered when your program is not doing anything. Therefore, your event is never called as your program is waiting indefinitely in the repeat..until loop inside StartClick.

    You should rewrite your program so that: