Exit Sub when KeyDown.Event is raised

176 views Asked by At

Simply said, I have a form with contains a button.
When the Click-Event is raised, a function is called which is connecting to a database and doing some more database-related stuff.
Now, sometimes the connection is taking its time to establish, and sometimes there is even no connection at all (which is fine and a common situation).
When this is happening, I want to give the user a opportunity to cancel whatever is happening.

A nice idea would be to cancel it upon a keydown-event (i.e. when pressing the ESC-key), so I added the KeyDown-Event and added this to it:

If (e.KeyCode = System.Windows.Forms.Keys.Escape) Then
    //cancel function called in button.Click
End If


But how exactly can I cancel whatever is going on/exit the button.Click-Sub?

1

There are 1 answers

0
OSKM On

With a similar issue i resolved it by:

  1. declared a Boolean
  2. created a cancel button
  3. Ran the time consuming code on a separate thread
  4. as there was a loop involved i checked the Boolean periodically (which was set via the cancel button click event).