Loop Interrupt

J

Jeremy Wood

Hello everyone;

I'm kind of new to VB and I am currently reading "Microsoft Visual Basic
..NET Step by Step" by Michael Halvorson. It's a great book and has been
very easy to read. The last time I did anything in VB was back in 1996 when
I was in college. (Wow has it been that long...)

Anyways, I apologize if this is such a n00b question.

I have a while loop that is reading data and writing to a file via
StreamWriter. This while loop is to continue until a variable changes from
1 to, well, anything else actually. On my form I have a start and stop
button for this action. The start button sets said variable to 1 and then
executes the while loop gathering data and writing it to file. The stop
button simply sets the aforementioned variable to 0. However, once the
while loop starts it never gives control back. You never have a chance to
hit the stop button! I end up having to kill the program. I'm sure this is
something stupid. What am I doing wrong?

Jeremy
VB .n00b
 
Q

Qwert

Place the following code inside the loop:

Application.DoEvents()

Windows will handle events now, such as you clicking on the stop button.
 
N

not aaron

you might want to look into using "threads". I am no expert in that
area, it just sounds like thats what you are needing. I have no code
examples, but i know you can search for "vb.net threads" and am sure
you'll get some ideas.

hope that helps a little bit at least.
 
R

Rob Windsor [MVP]

Add a call to Application.DoEvents() at the end of your loop. This will slow
down the execution of the loop somewhat but it will allow the stop button
event handler to set the variable to 0.
 
J

Jeremy Wood

Thanks guys! I guess I haven't learned that statement yet. I need to read
alot more I guess.

Jeremy Wood
VB .n00b
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top