Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Ending A never ending loop with key stroke

Posted on 2005-03-19
9
Medium Priority
?
212 Views
Last Modified: 2010-04-23
hey all.. I have written a never ending loop in visual basic .NET and i want to have an escape key can anyone tell me how to do this?
0
Comment
Question by:Sony-Controlled
9 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 13584102
you would have to do your loop in a separate thread and then cancel the thread on the keystroke
0
 
LVL 1

Author Comment

by:Sony-Controlled
ID: 13584116
and how would one go about that the source is

Do
Shell ("notepad.exe")
Shell ("cmd.exe")
Shell ("mspaint.exe")
Loop Until Shell ("notepad.exe") = 0 and Shell ("cmd.exe") = 0 and Shell ("mspaint.exe") = o
0
 
LVL 1

Author Comment

by:Sony-Controlled
ID: 13584120
you know the source  of wat to do for the key stroke would be help full
0
Technology Partners: 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!

 
LVL 12

Expert Comment

by:S-Twilley
ID: 13584973
Here's an example of one way to do it:

Public Class LoopConstruct
        Public ExitFlag As Boolean = False
        Public IsRunning As Boolean = False
        Public Sub Start()
                ExitFlag = False

                Dim thisThread As New Threading.Thread(AddressOf LoopSub)
                thisThread.Start
        End Sub

        Private Sub LoopSub()
               IsRunning = True
               Do While Not ExitFlag
                    ' Code within your loop
               Loop
               IsRunning = False
        End Sub
End Class

=========================================

Then within you form somewhere...

        Dim myLoop As New LoopConstruct

        Sub Command_Click(...) ' or some other sub
                If Not myLoop.IsRunning Then myLoop.Start
        End Sub

        Sub Form1_KeyPress(...) ' doing this off the top of my head... obviously use the proper keypress event
                If KeyPressed = Esc Then     ' just pseudo code this bit
                       myLoop.ExitFlag = True
                End If
        End Sub
0
 
LVL 1

Author Comment

by:Sony-Controlled
ID: 13586008
Thank you S-twiley that all worked out but im getting one error its saying If KeyPressed = ALT then so on and so forth its saying Keypressed is not declared
0
 
LVL 14

Accepted Solution

by:
amyhxu earned 375 total points
ID: 13586647
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        If e.KeyValue = Keys.Menu Then  'Keys.Menu is Alt key
            myLoop.ExitFlag = True
        End If
End Sub
0
 
LVL 1

Author Comment

by:Sony-Controlled
ID: 13586879
ok all the code is good now but it wont kill the app does the form have to be open for it to work? becuase the endless loop is in the load
0
 
LVL 14

Expert Comment

by:amyhxu
ID: 13587438
Yes, the form must be open if you write the escape function in your form events.
0
 
LVL 1

Author Comment

by:Sony-Controlled
ID: 13588469
thank you both but i couldnt get it to wrok with my script i jsut made timers instead lol but yall both helped me a bunch S-twilley wrote the strip and amy finished it im going to give the points to the person who has the least but thank yall both
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

564 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