Solved

KeyPress to Stop Program

Posted on 2006-07-21
5
340 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 62

Accepted Solution

by:
Fernando Soto earned 125 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 62

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now