Trying to coordinate Start and Stop buttons

JEofVA

Member
Joined
May 21, 2009
Messages
14
Programming Experience
1-3
I'm very new to Visual Basic, and have just installed VB Express 2008 from a Sams book titled, "Teach Yourself Visual Basic 2008 in 24 Hours", by James Foxall. I've managed to make it through most of the book, which was very helpful in getting a quick jump start, but I find that I am still "not quite getting it" yet.

I've written a program that has a "Start" and "Stop" button which are suppose to control the logging of data through a serial port. When I click the Start button, the serial port gets initialized, opened, and then I go into a Do-Loop-Until loop to process the incoming data stream, and that is suppose to stop when the Stop button is pressed. However, I haven't figured out how to implement that.

I assume the code should look something like the following:
VB.NET:
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click

    ... initializing set up code here 

Do

    ... serial port processing code here 

Loop Until btnStop.IsPressed

I probably have to use the 'e' parameter, but I haven't been able to come up with the proper syntax. Of course, the problem may be more fundamental than the just the syntax....

Any assistance would be appreciated,

Jonathan
 
Welcome to the forum, why not just cancel the process in the stop button's click event code instead of the loop.
 
Well, I tried to do just that. I terminated the loop with a
Loop Until ComPort.IsOpen = FALSE​
And then I used the btnClose_Click event to shut down the ComPort. However, that didn't work because the Close button would not respond to clicking on it, and thus the ComPort never shut down. In fact nothing in the project would respond to being clicked on.

Maybe you have a different way in mind?

Jonathan
 
You can't tie up the UI thread with loop like that, continuous/lengthy work must be done on a secondary thread, look into using the BackgroundWorker component for that. SerialPort class also has events that can be used that will call you when data is received instead of you looping & polling.
 
Back
Top