Cancelling running Code

phillip

Active member
Joined
Aug 28, 2003
Messages
37
Hi

I want to be able to terminate my running code using the Escape Key.
How would i Go about doing this.

Phill
 
Hmmm

hadle the event form.keypress or form.keydown and check if its the escape key, just make sure that any long loops you have, have application.doevents in them. and when you exit the program (escape key) make sure to set the exit conditions for all your loops, example if the loop is
do while running
.....
loop
then under keypress event
if e.keyvalue = keys.esc.esc then
running = false
exit
end if
 
I Tried your code but could not get it to work, Can you explain a bit further as im a bit of a noob.

Thanks in advance
 
Maybe this little example helps ? the private sub could be also _KeyPress or _KeyDown.

What kind of running code would you like to halt ?

[Vb]Private Sub MyLovelyClass_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

If e.KeyCode = Keys.Escape Then

Put the running "code" stopper inside here

End If

End Sub[/VB]
 
Last edited by a moderator:
Im not sure but this is what I came up fast....


Code:
Private Sub MyLovelyClass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyPress

            If e.KeyCode = Keys.Escape Then
     
                MyBooleanVariable = False

            End If

        End Sub


Do While (MyBooleanVariable=True)

   This is your loop

Enddo

ack... I think this doesnt work because it is busy in the loop maybe... damn my head is mixed up because I used to code in Atmel and it was kind of statemachine coding...
 
Last edited by a moderator:
I think this will work but i need to have it work from a button.
This is what i was using to goto the old routine but it no longer functions?

Private Sub Update1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Update1.Click
MyLovelyClass_KeyUp()
End Sub
 
Umm, Im not sure if I understand what you want but erm....
If you press ESC it will press btnStop on your form...


Code:
Private Sub MyLovelyClass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyPress

            If e.KeyCode = Keys.Escape Then
     
                 btnStop.PerformClick()

            End If

        End Sub

how is this ?
 
Maybe im better of explaining what im doing again.

I have a button that when clicked goes to my SUB RUNMYCODE

Using RUNMYCODE()

I then have an Array of names

I use

For each myname in names

Write a macro file and execute it in a Software package

Next myname


I want to be able to exit the Code by using the escape key
in the for each loop because the operation can take up to an hour.
 
Code:
Dim flag as boolean = false

For each myname in names

Private Sub MyLovelyClass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyPress

            If e.KeyCode = Keys.Escape Then
     
                 flag = true

            End If

        End Sub

if flag = true then

   Exit

end if


Next myname


lol try this.... I have no idea if it works.. now I wonder if it exits the IF END IF or the for loop... ack...

You could also try to put the private sub outside of the for each loop....

I got an new idea... how about if you cause an exception and exit the code with it ?
 
Last edited by a moderator:
Code:
Dim lol as byte 0-255


Try

For Each myname In names

Private Sub MyLovelyClass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyPress

            If e.KeyCode = Keys.Escape Then
     
                 lol = 1337 causes an overflow exception

            End If

        End Sub




Next myname

catch ex as exception

end try

I think my ideas are mad atm... can someone help maybe ?
 
Last edited by a moderator:
That didnt Seem to work for me.
Thanks very much for trying though.

Ive given up for the day :D
 
I looked through some of my old vb6 code and found thid

Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Function GetPressedKey() As String

For cnt = 32 To 180
Get the keystate of a specified key
If GetAsyncKeyState(cnt) <> 0 Then
GetPressedKey = Chr$(cnt)
Exit For
End If
Next cnt
End Function

Sub Main()

KeyPressed = GetPressedKey
If KeyPressed <> "" Then
Select Case KeyPressed
Case "Q", "q"
GoTo Quit
end select

I cant get it to work in vb.net though
 
Have you considered to add the "application.doevents" line to your loop ?

Possibly your application grabs up 100% CPU power, and no other processes can do anything, especially the system. This will result in a "deaf" program, because KeyPress events will takes ages until they are handled.
 
Back
Top