Unexplained Endless Loop

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Unexplained Endless Loop

Post by JRL » Thu Feb 04, 2010 6:44 pm

If you run this script, it goes into an endless loop and I don't understand why. To try and understand it I inserted a **BREAKPOINT** on line 45, ran the script in the editor, then stepped through a few lines to watch what happens.

What I see is that the script completes a Repeat> loop and a few lines later ends a subroutine. At the end of the subroutine for some unknown reason the script jumps back up to the Repeat> loop Then cycles forever. I know why it cycles forever, I don't understand why it jumps back to the Repeat> loop.

To see this script anomaly:
- Open the script in the editor
- Select any jpg or bmp image file (Preferably something small)
- Hold the CTRL key and pick anywhere on the displayed image.

At this point the script should hit the **BREAKPOINT**. Press F8 to step through the next few lines 'til the end of the subroutine. The next line after the end of the subroutine will be the Repeat> line. Why?

I'm running Macro Scheduler version 11.1.19 on Windows XP SP3

Can anyone confirm this behavior?

Update:
I changed the select Subroutine so that instead of a Repeat> function it uses a label> and a goto> and now when the script reaches the end of the subroutine, it jumps back up to the label. I'm still confused.

Code: Select all

SRT>Select
  If>SelectFlag=1
    GetCursorPos>CurX,CurY
    GetPixelColor>CurX,CurY,TransColor0
    GoSub>ConvertColor,TransColor0
    Let>NewC=0
    Label>Repeat_NewC
    Add>NewC,1
    Add>red,1
    RGB>Red,Grn,Blu,cValue
    Let>TransColor%NewC%=cValue
    If>%NewC%=%range%
    Else
      Goto>Repeat_NewC
    EndIf

**BREAKPOINT**

    SetDialogObjectColor>Dialog9,,TransColor
      Let>dialog9.mslabel1=%TransColor%
      ResetDialogAction>Dialog9
    Let>SelectFlag=2
  EndIf
END>Select

Code: Select all

//One time only acquire title bar size
Dialog>TitleBarSizeCalculation
Button=1,0,0,75,25,0
EndDialog>TitleBarSizeCalculation
Let>WIN_USEHANDLE=1
GetWindowPos>TitleBarSizeCalculation.msButton1.handle,OffSetX,OffSetY
Let>WIN_USEHANDLE=0


Let>msg_xpos=800
Let>msg_ypos=0
Let>Xpos=CENTER
Let>Ypos=0
Let>range=30

//VK1 = Mouse pick button
OnEvent>Key_Down,VK1,2,Select
//VK27 = Esc
OnEvent>Key_Down,VK27,8,Quit
//VK192 = ~
OnEvent>Key_Down,VK192,8,EndProcessing

SRT>Quit
  Exit>0
END>Quit

SRT>EndProcessing
  Let>ChangeKK=%range%
END>EndProcessing

SRT>Select

  If>SelectFlag=1
    GetCursorPos>CurX,CurY
    GetPixelColor>CurX,CurY,TransColor0
    GoSub>ConvertColor,TransColor0
    Let>NewC=0
    Repeat>NewC
      Add>NewC,1
      Add>red,1
      RGB>Red,Grn,Blu,cValue
      Let>TransColor%NewC%=cValue
    Until>NewC,%range%

**BREAKPOINT**

    SetDialogObjectColor>Dialog9,,TransColor
      Let>dialog9.mslabel1=%TransColor%
      ResetDialogAction>Dialog9
    Let>SelectFlag=2
  EndIf
END>Select


Input>image,Select a jpg or bmp file to display then recolor
If>image=
  Exit>0
EndIf

//Thank you twardega for this script that captures image size.
VBSTART
  Function ImgDimensions (ImgFile)
  dim myImg, fs
  Set fs= CreateObject("Scripting.FileSystemObject")
  If fs.fileExists(ImgFile) Then
  Set myImg = Loadpicture(ImgFile)
  iWidth = round(myImg.width / 26.4583)
  iHeight = round(myImg.height / 26.4583)
  Set myImg = nothing
  ImgDimensions = iWidth & "X" & iHeight
  End If
  End Function
VBEND

VBEval>ImgDimensions("%Image%"),Size
Separate>size,X,size

Let>Wide={%size_1%+%OffSetX%+%OffSetX%}
Let>Tall={%size_2%+%OffSetY%+%OffSetX%}

Dialog>Dialog5
   Caption=Image
   Width=%Wide%
   Height=%Tall%
   Top=%Ypos%
   Left=%Xpos%
   Image=%image%,0,0,%size_1%,%size_2%,changed,0
EndDialog>Dialog5

Dialog>Dialog9
   Caption=Dialog9
   Width=360
   Height=67
   Top=0
   Left=500
   Edit=msEdit1,120,15,150,%range%
   Label= ,10,20,true
EndDialog>Dialog9

Show>Dialog9
Show>Dialog5

Label>Loop
  Let>SelectFlag=0
  If>selectFlag=0
    GoSub>Process
  EndIf
  Wait>0.01
Goto>Loop

SRT>Process
  GetDialogAction>Dialog9,res9
  Let>Range=%dialog9.msedit1%
  ResetDialogAction>dialog9
  Let>SelectFlag=1
  Let>WIN_USEHANDLE=1
  SetFocus>Dialog1.handle
  Let>dialog3.mslabel1=%Dialog1.handle%
  Let>WIN_USEHANDLE=0
  Message>Select old color
  //GoSub>Select
  Label>Select1
  Wait>0.01
  If>SelectFlag=1,Select1
  Message>Select new color
  //GoSub>Select
  Label>Select2
  Wait>0.01
  If>SelectFlag=2,Select2
  ResetDialogAction>dialog3
  CloseDialog>Dialog1
  CloseDialog>Dialog3
END>Process

SRT>ConvertColor
  //Thank you me_again for
  //this code that converts a
  //color number to RGB values
  Let>px=ConvertColor_var_1
  Let>px2={%px% MOD 16}
  Let>px3={(%px%-%px2%)/16}
  Let>px4={%px3% MOD 16}
  Let>red={%px2%+(%px4%*16)}
  Let>px5={(%px3%-%px4%)/16}
  Let>px6={%px5% MOD 16}
  Let>px7={(%px5%-%px6%)/16}
  Let>px8={%px7% MOD 16}
  Let>grn={%px6%+(%px8%*16)}
  Let>px9={(%px7%-%px8%)/16}
  Let>px10={%px9% MOD 16}
  Let>px11={(%px9%-%px10%)/16}
  Let>px12={%px11% MOD 16}
  Let>blu={%px10%+(%px12%*16)}
////////////////////////////
END>ConvertColor

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Post by gdyvig » Fri Feb 05, 2010 4:11 am

Hi Dick,

I tried your code earlier and it did not have the endless loop.

Make sure NewC is incrementing as expected.

Make sure there are no trailing spaces.

I did have a little trouble getting the Select SRT to trigger, so I did the ctrl-click several times. So there is a possibility of triggering the SRT multiple times where none of the instances runs to completion. May depend on timing.

Try changing ConvertColor to inline code in the Select subroutine. When you do a gosub from within a OnEvent SRT the event may retrigger before the first instance of the Select subroutine finishes. However I would expect the code to cycle from the beginning of the Select SRT, not the Repeat loop.


Gale

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Feb 05, 2010 2:42 pm

I tried your code earlier and it did not have the endless loop.
Thanks Gale.

Oddly, When I download the script from the forum, it worked for me also. Rather than pursue the anomaly, I'll just go with the solution.

I Pasted the original script (much longer than what I posted) into notepad, deleted the original script, started a new script and pasted to it from notepad. that seems to have resolved the issue.

Must have been something wrong with the file itself. Though when I looked at it in a hex editor I found no odd characters.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts