Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...