Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

KeyPress to Stop Program

Posted on 2006-07-21
5
Medium Priority
?
346 Views
Last Modified: 2011-10-03
I would like to allow user to stop execution of a subroutine by hitting the Escape key.  The subroutine is a program module that is called from a form.  I can make KeyPress events work within the calling form, but they arent trapped while the subroutine is running.
Bill
0
Comment
Question by:BW999
5 Comments
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17155418
You'll have to Thread your subroutine if you want to capture key events while it is executing.

  Dim t As System.Threading.Thread

    Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Start the subroutine Thread
         t = New System.Threading.Thread(AddressOf Me.MyProcess)
         t.Start()
    End Sub

    Private Sub MyProcess()
         .
         'Put your subroutine code here
         .
    End Sub

    Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Escape Then
            t.Abort()
            MsgBox("SUBROUTINE ABORTED")
        End If

        Return MyBase.ProcessDialogKey(keyData)
    End Function
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17155467
Hi BW999;

In the subroutine you must have a loop. While the system is running this subroutine no events are being processed. Place a Application.DoEvents() in the loop in the subroutine. It should do the trick.

Fernando

0
 
LVL 3

Expert Comment

by:HouseMusic
ID: 17156693
btw... if you want it to work if your application is not active you are going to have to do a global keyboard hook
0
 

Author Comment

by:BW999
ID: 17156967
Fernando's Answer - short & sweet.  Thanks.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17157015
No problem, glad I was able to help. ;=)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Loops Section Overview
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question