Home All Groups Group Topic Archive Search About

Re: Newbie needs help temporarily pausing loop

Author
27 Mar 2006 8:09 AM
eejit
Thanks Randy,


The thread.sleep(timeout.infinite) works great at pausing the program,
but do I have to create a seperate thread to handle a click event to
restart it?

I'm a complete hack at this, just trying to throw together a small
program,, and after reading about this threading stuff, I think my
head is going to explode.



On Sat, 25 Mar 2006 14:17:51 +0100, "R. MacDonald"
<sci***@NO-SP-AMcips.ca> wrote:

Show quoteHide quote
>Hello, eejit,
>
>Look at the Thread.Sleep method (System.Threading namespace).
>
>You will probably want to use "Thread.Sleep(Timeout.Infinite)" in this case.
>
>Cheers,
>Randy
>
>
>eejit wrote:
>
>> My code reads a text file and stores each line of text in an array.
>>
>> I then loop through the array showing each line as a label.
>>
>> At a certain point in the loop, I'd like to call a sub that pauses the
>> loop until the user clicks a button, at which point the loop
>> continues.
>>
>> eg
>>
>> for x = 1 to 20
>>
>> label1.text = array(x)
>>
>> if x = 10 then call pausesub()
>>
>> next x
>>
>>
>> thanks for any help
>>
>> eejit

Author
27 Mar 2006 8:24 AM
Josef Brunner
Hi Randy,

"eejit" <ee***@canada.com> schrieb im Newsbeitrag
news:o87f2215rhunmtp7ktmsi3rojua97eldpe@4ax.com...
> Thanks Randy,
>
>
> The thread.sleep(timeout.infinite) works great at pausing the program,
> but do I have to create a seperate thread to handle a click event to
> restart it?

one solution is to have a (member) variable and a loop where you always
sleep some time ... see this simple example:


Dim Counter As Integer = 0

Do While ConfigLoaded = False

Counter += 1

' Maximum time we wait is: 40 x 1000 Milliseconds = 40 seconds!

If Counter >= 40 Then

Exit Do

End If

If MyStopVariableIsTrue Then

MsgBox("Stopping the wait loop")



Exit Sub

End If

My.Application.DoEvents()

Threading.Thread.Sleep(1000)

Loop



Greets,

Josef
Author
27 Mar 2006 10:26 AM
Stephany Young
Using Thread.Sleep with an infinite timeout is not a good idea is this case.

Not only does it put your loop to sleep forever, it also puts the entire
thread to sleep forever.

In the abscence of any evidence to the contrary, I am assuming that your
loop is somewhere in your UI thread and therefore your entire UI will be
suspended forever. This means that you will not be able to click your
button.

I would be inclined to use event driven nature of your application to your
advantage in conjunction with a simple re-entrant code technique.

If you have a class-level variable that contains the start value for your
loop then the whole thing becomes:

  Private m_start As Integer = 1

  Private Sub XXXX()

    For x As Integer = m_start To 20
      Label1.Text = array(x)
      If x = 10 Then
        m_start=11
        Exit For
      End If
    Next

    If x > 20 Then m_start = 1

  End Sub

In the handler for the click event of your button, simply call XXXX.

The first time you call XXXX then is business as usual until it has
displayed the 10th array element. When this point is achieved the code exits
the loop (after setting the loop start variable) and the procedure, thus
freeing up the UI thread.

When your button is clicked, the call to XXXX means that the loop starts
from 11 and displays the remaing 10 array elements before exiting the loop
by dropping through the logic. This time the loop start variable is reset to
1 because the loop controller has passed it's limit of 20.

Another call to XXXX will start the whole thing over again.

Another point: The display of the array elements in the label will happen so
fast that the user will not be able to see them. You may need another delay
to slow the loop down.


Show quoteHide quote
"eejit" <ee***@canada.com> wrote in message
news:o87f2215rhunmtp7ktmsi3rojua97eldpe@4ax.com...
> Thanks Randy,
>
>
> The thread.sleep(timeout.infinite) works great at pausing the program,
> but do I have to create a seperate thread to handle a click event to
> restart it?
>
> I'm a complete hack at this, just trying to throw together a small
> program,, and after reading about this threading stuff, I think my
> head is going to explode.
>
>
>
> On Sat, 25 Mar 2006 14:17:51 +0100, "R. MacDonald"
> <sci***@NO-SP-AMcips.ca> wrote:
>
>>Hello, eejit,
>>
>>Look at the Thread.Sleep method (System.Threading namespace).
>>
>>You will probably want to use "Thread.Sleep(Timeout.Infinite)" in this
>>case.
>>
>>Cheers,
>>Randy
>>
>>
>>eejit wrote:
>>
>>> My code reads a text file and stores each line of text in an array.
>>>
>>> I then loop through the array showing each line as a label.
>>>
>>> At a certain point in the loop, I'd like to call a sub that pauses the
>>> loop until the user clicks a button, at which point the loop
>>> continues.
>>>
>>> eg
>>>
>>> for x = 1 to 20
>>>
>>> label1.text = array(x)
>>>
>>> if x = 10 then call pausesub()
>>>
>>> next x
>>>
>>>
>>> thanks for any help
>>>
>>> eejit
>