KiXforms Forum Index KiXforms
The Forum for the KiXforms Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Quick Links 
Site News
Downloads
Documentation
Donations
Script Archive
Tracking Systems

Checking for keypress in nested loop when no form has focus.

 
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion
View previous topic :: View next topic  
Author Message
MACE1
KiXforms Enthusiast
KiXforms Enthusiast


Joined: 12 Oct 2004
Posts: 130
Location: Manchester UK

PostPosted: Wed Jun 13, 2007 8:05 pm    Post subject: Checking for keypress in nested loop when no form has focus. Reply with quote

I have a form which following input calls a function with a tripple nested loop. This loop can take several minutes to run. As none of the areas of the form have focus during the loops it would appear wherever I put a keypress event, it will not run until the nested loops finish. I tried the old console kbHit() but is is not useable either as no keys in the buffer.
Has anyone a suggestion how I can trap a chr(27) should I wish to manually break out of the loops without having to completely close the app.
? Would a timer event run regardless of the loop ? Will have a play...
________
Arizona marijuana dispensaries


Last edited by MACE1 on Fri Feb 18, 2011 5:11 pm; edited 1 time in total
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Wed Jun 13, 2007 9:58 pm    Post subject: Reply with quote

Good question, really good question ...

Was just completing this sample script but have to leave until later tonight ... hopefully you can check it out to see whats going on, whats happening ... the heavy duty loop is in Button1_StartClick() ... and i am using the special async version of DoEvents (nowait)

Heres the script, can discuss later:

Code:

Break On

$System = CreateObject("Kixforms.System")

$Form = $System.Form()

$Label1 = $Form.Controls.Add($System.Label())
$Label1.FontSize = 15
$Label1.Text = "0000"
$Label1.Center

$Button1 = $Form.Controls.Add($System.Button())
$Button1.Center
$Button1.Top = $Label1.Bottom
$Button1.Left = $Label1.Left
$Button1.Text = "Start"
$Button1.Click = "Button1_StartClick()"

$Form.Center
$Form.Show
While $Form.Visible
 $= Execute($Form.DoEvents)
Loop

Exit 0

Function Button1_StartClick()

 $Label1.Text = "0000"
 $Button1.Text = "Stop"
 $Button1.Click = "Button1_StopClick()"

 While $Button1.Text = "Stop"

  sleep 0.010
  $Label1.Text = Val($Label1.Text) + 1
  $= Execute($Form.DoEvents(0))
 
 Loop

EndFunction

Function Button1_StopClick()

 $Button1.Text = "Start"
 $Button1.Click = "Button1_StartClick()"

EndFunction
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Thu Jun 14, 2007 2:46 pm    Post subject: Reply with quote

Here is a sample that uses KeyPresses to interupt a tight loop (with the async doevents inside - which is the key to this working).

Code:

Break On

$System = CreateObject("Kixforms.System")

$Form = $System.Form()
$Form.KeyPreview = 1
$Form.KeyPress = "Form_KeyPress()"
$Form.Tag = 0

$Label1 = $Form.Controls.Add($System.Label())
$Label1.FontSize = 15
$Label1.Width = 150
$Label1.BackColor = $System.Color.PaleGreen
$Label1.Text = "0000"
$Label1.Center

$Button1 = $Form.Controls.Add($System.Button())
$Button1.Left = $Label1.Left
$Button1.Top = $Label1.Bottom + 5
$Button1.Width = $Label1.Width
$Button1.Text = "Click me to start"
$Button1.Click = "Button1_Click()"

$Form.Center
$Form.Show
While $Form.Visible
 $= Execute($Form.DoEvents)
Loop

Exit 0

Function Button1_Click()

 $Form.Tag = 0
 $Button1.Text = "Press any key to stop"

 While $Form.Tag = 0

  sleep 0.10
  $Label1.Text = Val($Label1.Text) + 1
  $= Execute($Form.DoEvents(0))
 
 Loop

EndFunction

Function Form_KeyPress()

 $Form.Tag = 1
 $Button1.Text = "Click me to start"

EndFunction
Back to top
View user's profile Send private message
MACE1
KiXforms Enthusiast
KiXforms Enthusiast


Joined: 12 Oct 2004
Posts: 130
Location: Manchester UK

PostPosted: Fri Jun 15, 2007 3:59 pm    Post subject: Reply with quote

Understand what you are doing.
Currently using For - Next loops, so will change to While loop with a counter and will report back how it goes when I get time to look at it.

NB: Can anyone remember the command to clear the content of a listbox...
________
Toyota group history


Last edited by MACE1 on Fri Feb 18, 2011 5:11 pm; edited 1 time in total
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Fri Jun 15, 2007 5:39 pm    Post subject: Reply with quote

Dont think you need to re-engineer your loops too much - with counters etc - just need to get this:

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

Somewhere within your deeply nested loop. And I just put the sleep command in there so that CPU usage didn't go to 100% (because the loop really wasn't doing anything meaningful).
Back to top
View user's profile Send private message
MACE1
KiXforms Enthusiast
KiXforms Enthusiast


Joined: 12 Oct 2004
Posts: 130
Location: Manchester UK

PostPosted: Sat Jun 16, 2007 9:23 am    Post subject: Reply with quote

I set an event for each part of the form to call a function which checks if chr(27) is pressed. Depending on where in the form will depend on what to do about it.
In this case it sets a global flag for STOP which is checked on every pass within each of the three loop.
If STOP is confirmed then to terminate correctly I simply set the loop counter to above the last value so NEXT exits correctly and things carry on cleanly.

While in the main loops, if an ESC is pressed it now pops a messagebox within a couple of seconds rather than waiting to the end of the loops.
Thanks for that. I did not know about the timing part of the DoEvents(0)
________
MERCEDES-BENZ 300
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group