Restart thread

kejpa

Junior Contributor
Joined
Oct 10, 2003
Messages
320
Hi,
I have a really quick routine for finding next file to read, but I thought it would be great to have it running in the background and restart it when it's needed.

I tried to create the thread in the constructor and call it for the first time from there.
Code:
        _trdFindNextFile = New Threading.Thread(AddressOf FindNextFile)
        _trdFindNextFile.Start()
And when I need it again I tried
Code:
        _trdFindNextFile.Start()
Code:
    Private Sub FindNextFile()
        Trace.WriteLineIf(trcMainTrace.Info, "Looking for next log file", "cReadDemo.FindNextFile")
        _NextFile = _File
        _trdFindNextFile.Join(0)
    End Sub

But I get an error telling me the thread is either running or stopped and can't be restarted :(

It's obviously not running, how should I restart it?!?

TIA
/Kejpa
 
You can only call Start once. I would make FindNextFile go in an infinite loop, but use a WaitHandle to be more efficient with cpu usage.

I don't know vb, so my example isn't the best.
Code:
Public Sub LookForNext()
    _waitHandle.Set()
End Sub

Private Sub FindNextFile()
    While True
        If _waitHandle.WaitOne() Then
            Trace.WriteLineIf(trcMainTrace.Info, "Looking for next log file", "cReadDemo.FindNextFile")
            _NextFile = _File
            _trdFindNextFile.Join(0)
        End If
        _waitHandle.WaitOne()
    End While
End Sub

You would start your thread like normal, but instead of making more calls to Thread.Start, use the method LookForNext.
 
Back
Top