Solved

KeyPress to Stop Program

Posted on 2006-07-21
5
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 63

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 63

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

635 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