Page 1 of 1 1
Topic Options
#89702 - 2002-11-22 06:09 PM Kixforms: Breaking out of a loop/Function with a button
krabourn Offline
Hey THIS is FUN
*****

Registered: 2000-12-11
Posts: 244
Loc: San Antonio, Texas, USA
I a script that performs different actions on a list of computers. I want to use an "Emergency Stop" button to stop whatever action on the computers. When I click on the button, it is ignored until the list is finished.

How can you break out of a Function/Loop using a button on a form?

here is an example of pinging.
code:
FUNCTION Ping_Click()
$ButtonStop.OnClick = "GOTO 'Ping_ClickEnd'"
ResetList_Click()
FormBeginUpdate()
$RC = $List.Columns.Add('Ping Status')
$RC = $List.Columns.Add('Ping IP')
$RC = $List.Columns.Add('Ping Name')
$RC = $List.Columns.Add('Ping Error')
FormEndUpdate()
FOR EACH $Item IN $List.Items
$LabelCountValue.Text = '' + $List.Items.Count + ' \ ' + Val ($Item.Index + 1)
$ProgressBarStatus.Value = $Item.Index + 1
$List.EnsureVisible($Item.Index)
$Item.Selected = 1
$RC = IsPing($Item.SubItems(0).Text)
$PingError = @error
SELECT
CASE $PingError = 0
$PingStatus = 'On'
CASE $PingError = 1
$PingStatus = 'RAS'
CASE $PingError = 2
$PingStatus = 'Off'
CASE $PingError = 3
$PingStatus = 'Gone'
CASE 1
$PingStatus = 'PROBLEM'
ENDSELECT
$Item.SubItems(1).Text = $PingStatus
$Item.SubItems(2).Text = $AddressIP
$Item.SubItems(3).Text = $AddressName
$Item.SubItems(4).Text = $IPError
$Item.Selected = 0
NEXT
:Ping_ClickEnd
FormBeginUpdate()
FormEndUpdate()
BEEP
ENDFUNCTION

I forgot. Here is some of the form function that are called.
code:
FUNCTION ResetList_Click()
IF $List.Columns.Count > 1
FOR $ColumnIndex = $List.Columns.Count - 1 TO 1 STEP -1
$List.Columns($ColumnIndex).Remove
NEXT
ENDIF
ENDFUNCTION

FUNCTION FormBeginUpdate()
$Form.MousePointer = 11
$Form.Enabled = 0
$Form.BeginUpdate
ENDFUNCTION

FUNCTION FormEndUpdate()
FOR EACH $Column IN $List.Columns
$Column.Width = -1
$Column.Width = -2
NEXT
$Form.EndUpdate
$Form.MousePointer = 0
$Form.Enabled = 1
$List.SetFocus
ENDFUNCTION

Thanks

[ 22. November 2002, 18:19: Message edited by: krabourn ]
_________________________
Kelly

Top
#89703 - 2002-11-22 06:37 PM Re: Kixforms: Breaking out of a loop/Function with a button
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You cannot break out of FOR/WHILE/DO loop easily as has been discussed in the 'suggestions' forum. We hope that e.g. EXIT FOR will be included in one of the enxt releases.

However, you can reorganize your loops in such a way as to check for some kind of abort event
code:
$abort=0
$start=1
$stop=10
while $start<$stop or not $abort
$start=$start+1
; check 'STOP' button status and set $abort to 1 if pressed
loop

_________________________
There are two types of vessels, submarines and targets.

Top
#89704 - 2002-11-22 06:51 PM Re: Kixforms: Breaking out of a loop/Function with a button
krabourn Offline
Hey THIS is FUN
*****

Registered: 2000-12-11
Posts: 244
Loc: San Antonio, Texas, USA
I was hoping I would not have to do that. I am a little surprised that the form's click event was not fully recongnized until the loop was finished.

Thanks
_________________________
Kelly

Top
#89705 - 2002-11-22 06:55 PM Re: Kixforms: Breaking out of a loop/Function with a button
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
krabourn, stick a DoEvents(1) inside the loop, and the click will be recognised.
Top
#89706 - 2002-11-22 07:02 PM Re: Kixforms: Breaking out of a loop/Function with a button
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Check out how I did it in CompPswdAge.kix.

code:
If $frmMain.Visible $nul=Execute($frmMain.DoEvents(1)) Else Exit() EndIf


Top
#89707 - 2002-11-22 07:25 PM Re: Kixforms: Breaking out of a loop/Function with a button
krabourn Offline
Hey THIS is FUN
*****

Registered: 2000-12-11
Posts: 244
Loc: San Antonio, Texas, USA
Okay here is some code.

When I click the button while inside the Ping function it outputs "ButtonStop_Click()" to the command line. The button works fine when called directly from the form.

At the beginning of the script.
code:
$SwitchStopProgress = 0

Here is the Button
code:
$ButtonStop = $Form.Button
$ButtonStop.Text = 'STOP'
$ButtonStop.Top = $LabelCount.Top
$ButtonStop.Left = $LabelCountValue.Right + 5
$ButtonStop.Size = 90,25
$ButtonStop.OnClick = 'ButtonStop_Click()'
$ButtonStop.ToolTip = 'Stop the current tasks progress'

The function for the button.
code:
FUNCTION ButtonStop_Click()
$SwitchStopProgress = 1
ENDFUNCTION

Here is the functinon
code:
FUNCTION Ping_Click()
$SwitchStopProgress = 0
ResetList_Click()
FormBeginUpdate()
$RC = $List.Columns.Add('Ping Status')
$RC = $List.Columns.Add('Ping IP')
$RC = $List.Columns.Add('Ping Name')
$RC = $List.Columns.Add('Ping Error')
FormEndUpdate()
FOR EACH $Item IN $List.Items
$LabelCountValue.Text = '' + $List.Items.Count + ' \ ' + Val ($Item.Index + 1)
$ProgressBarStatus.Value = $Item.Index + 1
$List.EnsureVisible($Item.Index)
$Item.Selected = 1
$RC = IsPing($Item.SubItems(0).Text)
$PingError = @error
SELECT
CASE $PingError = 0
$PingStatus = 'On'
CASE $PingError = 1
$PingStatus = 'RAS'
CASE $PingError = 2
$PingStatus = 'Off'
CASE $PingError = 3
$PingStatus = 'Gone'
CASE 1
$PingStatus = 'PROBLEM'
ENDSELECT
$Item.SubItems(1).Text = $PingStatus
$Item.SubItems(2).Text = $AddressIP
$Item.SubItems(3).Text = $AddressName
$Item.SubItems(4).Text = $IPError
$Item.Selected = 0
$Form.DoEvents(1)
? $SwitchStopProgress
IF $SwitchStopProgress = 1
GOTO 'Ping_ClickEnd'
ENDIF
NEXT
:Ping_ClickEnd
FormBeginUpdate()
FormEndUpdate()
BEEP
ENDFUNCTION

_________________________
Kelly

Top
#89708 - 2002-11-22 07:28 PM Re: Kixforms: Breaking out of a loop/Function with a button
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Have to wrap it in an Execute:

$= Execute($Form.DoEvents(1))

Top
#89709 - 2002-11-22 07:28 PM Re: Kixforms: Breaking out of a loop/Function with a button
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
basic sin...
doevents is just string keeping inside the actions which are pending to carry out.
so when you do:
$Form.DoEvents(1)

it will output that string.
correct line:
$=execute($Form.DoEvents(1))
_________________________
!

download KiXnet

Top
#89710 - 2002-11-22 07:29 PM Re: Kixforms: Breaking out of a loop/Function with a button
krabourn Offline
Hey THIS is FUN
*****

Registered: 2000-12-11
Posts: 244
Loc: San Antonio, Texas, USA
Never mind. I forgot to put it inside an execute.
_________________________
Kelly

Top
#89711 - 2002-11-22 07:29 PM Re: Kixforms: Breaking out of a loop/Function with a button
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
blaah, too slow.
_________________________
!

download KiXnet

Top
#89712 - 2002-11-22 07:32 PM Re: Kixforms: Breaking out of a loop/Function with a button
krabourn Offline
Hey THIS is FUN
*****

Registered: 2000-12-11
Posts: 244
Loc: San Antonio, Texas, USA
This is starting to look more and more like an instant messenger. I reply to an answer and get three more before I click "Add Reply". [Big Grin]

Good job Lonkero!! [Smile]
_________________________
Kelly

Top
#89713 - 2002-11-22 07:34 PM Re: Kixforms: Breaking out of a loop/Function with a button
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
btw, breaking out of loops is no hard thing to do when you modulize your scripts.
when every separate task has own function, it's easy as typing "exit 1"

{edit}
what comes to the messenger, we are working on it [Wink]

[ 22. November 2002, 19:36: Message edited by: Lonkero ]
_________________________
!

download KiXnet

Top
#89714 - 2002-11-22 07:39 PM Re: Kixforms: Breaking out of a loop/Function with a button
krabourn Offline
Hey THIS is FUN
*****

Registered: 2000-12-11
Posts: 244
Loc: San Antonio, Texas, USA
I guess I am brain dead today. I should have remembered "Execute". This should have taken about fifteen minutes.

Thanks
_________________________
Kelly

Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 895 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.065 seconds in which 0.027 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org