Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,072 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,779 people online right now. Registration is fast and FREE... Join Now!




Break out of loop from a keypress

 
Reply to this topicStart new topic

Break out of loop from a keypress, How do I break a running loop with a keypress

geek53
1 May, 2008 - 01:44 PM
Post #1

New D.I.C Head
*

Joined: 1 May, 2008
Posts: 4

I have been trying to find a way to break a loop that is continually polling a modem with a keystroke via a button control or just a ESC key. I'm trying to break the loop 'Do While x<4'. I would normally set it to run until a keystroke or button press by changing it to "Do while True".

Any help would be greatly appreciated.
ps. port1 is declared public in a module for storing the commport name ex:"COM1"

vb


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim x As Integer = 1
Do While x < 4
Dim n As Integer = 0
Dim strng2 As String = ""
Using serialport1 As SerialPort = My.Computer.Ports.OpenSerialPort(port1)
serialport1.DtrEnable = True
serialport1.RtsEnable = True
'Dim start, finish, totalTime As Double
Do While n < 3 '4 loops then exit and come back on timer
If TextBox1.Text = "" Then
Else
Dim strng As String = TextBox1.Text
serialport1.Write(strng + vbCrLf) ' All data transfer code goes here.
taketime(0.2)
TextBox1.Text = ""
End If
taketime(0.2)
If serialport1.BytesToRead = 0 Then
Else
strng2 = serialport1.ReadExisting ' All data transfer code goes here.
taketime(0.2)
TextBox2.Select()
TextBox2.Text = strng2
Exit Do
End If
n = n + 1
Loop
End Using
taketime(1) 'poll modem every 1 seconds
x = x + 1
'Dim ee As System.EventHandler
'If ee. = Chr(27) Then
' exit try
'End If
' my attempt above to catch esc key^whatsthat.gif'
Loop
SerialPort1.DtrEnable = False
SerialPort1.RtsEnable = False
SerialPort1.Close()
Catch ex As Exception
MsgBox("The port is in use, close the port and try again.")
Exit Try
End Try

SerialPort1.DtrEnable = False
SerialPort1.RtsEnable = False
SerialPort1.Close()
ProgressBar2.Value = 0

End Sub



This post has been edited by PsychoCoder: 1 May, 2008 - 02:52 PM
User is offlineProfile CardPM
+Quote Post

geek53
RE: Break Out Of Loop From A Keypress
6 May, 2008 - 05:44 AM
Post #2

New D.I.C Head
*

Joined: 1 May, 2008
Posts: 4

I found an answer. insert 'application.doevents()' in the loop and make a function to pass a variable that will be read by the do while loop as exit do. Put a call to a function that contains the variable to exit the do while in side the loop. Fire the function with a keypress or button. The doevents makes the form multithread.. works great.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:05PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month