?
Solved

Ending A never ending loop with key stroke

Posted on 2005-03-19
9
Medium Priority
?
209 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
[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
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
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!

 
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

Industry Leaders: 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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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