kejpa Posted December 22, 2005 Posted December 22, 2005 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. _trdFindNextFile = New Threading.Thread(AddressOf FindNextFile) _trdFindNextFile.Start() And when I need it again I tried _trdFindNextFile.Start() 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 Quote
HJB417 Posted December 22, 2005 Posted December 22, 2005 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. 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.