pausing a running while loop, and wait for certain button until the looping resumes

janus85_ren

Member
Joined
May 9, 2010
Messages
24
Programming Experience
1-3
I'd like to make a simple program where my form consists of a textbox and a button. Here's the ide in a nutshell:

in form1_load I invoke the sub:

private Sub LoopingLesson()

dim i as integer=1

while true
textbox1.text=i
i=i+1
end while

End Sub

what I want is when i hits certain value, like let's just say i is divisible by 365, then the 'while' looping will stop, and a message box saying "target reached" appear. then after I click button1, the looping will resume until it reaches the next number divisible by 365, and so on.

thanks
 
What you describe is not the way it would be done. It would be more like this:
VB.NET:
Private counter As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.DoLoop()
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.DoLoop()
End Sub

Private Sub DoLoop()
    Do
        'Do something here.

        Me.counter += 1
    Loop Until Me.counter Mod 365 = 0
End Sub
That said, you wouldn't execute that loop on the UI thread unless it was very fast, because it would freeze the UI otherwise.
 
OK, that would work for one level looping. what about nested looping?
because what u do, is restarting the looping form the begining. while what I want is the looping to continue from the last it stopped.
thanks
 
OK, that would work for one level looping. what about nested looping?
because what u do, is restarting the looping form the begining. while what I want is the looping to continue from the last it stopped.
thanks

I suggest that you look at the code a bit closer. Maybe you could even run it yourself.
 
What I'm trying to do really is translating C++ code to vb.net. I hope you're familiar with C++:

for(i=0;i<top;i++){
for(j=0;j<i*i;j++){
if(!kbhit()){
// do something
}
else getch();
}
}

So, I'm having problem translating kbhit part. because you see with kbhit(), the program stops for a while and resumes after another button is hit again.
and the looping isn't restarted from the beginning. Yes, I know your code resembles this behavior by using global variable. what I'm trying to do here is to use local variable. thanks for the help.
I've searched the net, but what I found was thread programming (Thread.Sleep). this method causes the entire to program to freeze.
I also see some suggestions to use DoEvents. but it is said to consume a lot of resources. so I wont make it my first choice.
 
Last edited:
From what I understand of kbhit, what you're saying is not correct.

kbhit - C++ Function Reference
It returns a non-zero integer if a key is in the keyboard buffer. It will not wait for a key to be pressed.
kbhit doesn't wait at all. It returns immediately and simply tells you whether a key has been pressed or not.
 
Generally I'm generating all possible Teaching schedule in a school. So everytime the application produce a schedule that meets all the constraints, the schedule will be displayeed in a datagridview. the user is free to see and asses the schedule, and if it doesnt suit him, he can just hit the next button, and the application will continue the looping until the next best schedule is obtained. thanks
 
Perhaps I should explain a bit about the scheduling procedure:

I'm generating schedules for primary and secondary students. This is the condition:
we have 10 levels (P1 - P6 and Sec 1 - Sec 4)
Each level may have several classrooms (P1 may have P1A, P1B, P1C, etc)
Each class rooms have 10 subjects (actually, it could be more) to be taught.
The school (for all students) has 5 school days.

but there are some constraints:
lets just say subject Math for P1A. this particular subject must be taught 15 shifts a week. this load of 15 must be spread within the 5-day period.

I can only think of recursive method. At first I thought I could make a looping system that mimics the recursive method, but apparently I was wrong. That's why I'm searching a way to pause within the looping, hoping that it can be used for pausing the recursive when the user is assessing the generated schedule before he decides to generate the next schedule
 
Back
Top