tags:

views:

139

answers:

2

Hi,

I have a simple code that looks up a text file, reads the line of text, splits the string by semi-colons and then posts the results.

After it has done this, I have created a really simple while loop to waste 10 seconds before going for it again.... here is the code:

Private Sub checkTemps()
    While Abort = False
        Try
            fileReader = New StreamReader(directory.Text & "currentTemp.dat")
            rawData = fileReader.ReadLine()
            fileReader.Close()
            Dim dataArray() As String
            dataArray = rawData.Split(";")
            updateOutput("1", dataArray(0), dataArray(1))
            updateOutput("2", dataArray(2), dataArray(3))
            updateOutput("3", dataArray(4), dataArray(5))
            updateOutput("4", dataArray(6), dataArray(7))
            stpWatch.Start()
            While stpWatch.Elapsed.Seconds < 10 And Abort = False
                pollInterval(stpWatch.ElapsedMilliseconds)
            End While
            stpWatch.Stop()
            stpWatch.Reset()
        Catch ex As Exception
            msgbox("oops!")
        End Try
    End While
    closeOnAbort()
End Sub

But when it gets to the "time-wasting" loop - it seems to slow the whole application down? And I can't work out why!

So a couple of questions... is there a better way to do all this? and second - can anyone spot a problem?

All the other commands seem to run fine - there isn't much else to this app. I have another program that updates the dat file with the values, this is simply a client side app to output the temperatures.

Any help would be appreciated.

Andrew


More info:

I should explain what the pollInterval sub does!

Private Delegate Sub pollIntervalDelegate(ByVal value As Integer)
Private Sub pollInterval(ByVal value As Integer)
    If Me.InvokeRequired Then
        Dim upbd As New pollIntervalDelegate(AddressOf pollInterval)
        Me.Invoke(upbd, New Object() {value})
    Else
        ProgressBar1.Value = value
    End If
End Sub
A: 

You should go with

System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10).TotalMilliseconds);
Rubens Farias
But I want to show the length of time until the next refresh... if I put the thread to sleep - I won't be able to show this - will I?
Blind Trevor
You'll need to define a update interval; Add a `Thread.Sleep(1000)` inside your `while` loop and it will wait for a second before every screen update
Rubens Farias
So you mean - While stpWatch.Elapsed.Seconds < 10 And Abort = False | pollInterval(stpWatch.ElapsedMilliseconds) | Thread.Sleep(1000) | End While - ?
Blind Trevor
I shall try it - thanks for your help :)
Blind Trevor
+2  A: 

Your loop is a very tight loop continually calling pollInterval. This will tie up the application until the loop condition is met.

You should use the Sleep method to pause this thread for the required amount of time.

If you want to show the progress (as per your update) you could put the Sleep into the loop and sleep for 1 second (or half a second?) at a time:

        While stpWatch.Elapsed.Seconds < 10 And Abort = False
            Sleep(1000)      <-- NOT 100% sure of the syntax here,
                                 but the time is specified in milliseconds
            pollInterval(stpWatch.ElapsedMilliseconds)
        End While
ChrisF
What would you suggest?I want to be able to show the remaining time before the next update using a progress bar... is there a better way?
Blind Trevor