Need Help On Restarting Threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pbd22

    #1

    Need Help On Restarting Threads

    Hi.

    I am getting the error: "Thread is running or terminated. It cannot
    restart." It is happening inside a file upload loop where
    a thread is created for each file (reporting bytes). After the first
    file is uploaded, the error is thrown at workThread.Star t().

    I am new to threading and would appreciate advice on how to code this
    so when the next file is starting to upload, the thread is restarted
    (reinstantiated ).

    Below is a little on the function and the code:

    ---------------------------------------------------------

    I have a function SendVideo().

    Inside the function I have a thread that counts bytes:

    Code:

    Dim workThread As New Thread(New ThreadStart(Add ressOf ftp.doWork))



    Further down, I have a loop that starts the thread before the
    upload begins and calls join after each upload:

    Code:

    For iFile = 0 To myfiles.Count - 1

    workThread.Star t()
    ftp.UploadFile( postedFile.File Name)
    workThread.Join ()
    End If

    Next iFile



    workThread.Star t() above is where the error is thrown after the first
    upload.

    Thanks....

  • Branco Medeiros

    #2
    Re: Need Help On Restarting Threads

    pbd22 wrote:
    I am getting the error: "Thread is running or terminated. It cannot
    restart." It is happening inside a file upload loop where
    a thread is created for each file (reporting bytes). After the first
    file is uploaded, the error is thrown at workThread.Star t().
    <snip>
    Dim workThread As New Thread(New ThreadStart(Add ressOf ftp.doWork))
    <snip>
    For iFile = 0 To myfiles.Count - 1
    >
    workThread.Star t()
    ftp.UploadFile( postedFile.File Name)
    workThread.Join ()
    End If
    >
    Next iFile
    >
    workThread.Star t() above is where the error is thrown after the first
    upload.
    <snip>

    As the error says, the thread has already run, it seems you can't
    'restart' it. I guess you'll have to recreate the thread at each
    cicle.

    Anyway, it seems that there are other issues with your code. I
    couldn't identify the class you're using as "ftp", but it seems that
    you're starting the thread to "doWork" while also commanding the ftp
    object to "UploadFile ". This seems a bit confusing: which one is
    actually performing the download? Maybe you could post more
    information about the kind of classes you're using.

    Regards,

    Branco.

    Comment

    • pbd22

      #3
      Re: Need Help On Restarting Threads

      On Feb 28, 10:38 am, "Branco Medeiros" <branco.medei.. .@gmail.com>
      wrote:
      pbd22 wrote:
      I am getting the error: "Thread is running or terminated. It cannot
      restart." It is happening inside a file upload loop where
      a thread is created for each file (reporting bytes). After the first
      file is uploaded, the error is thrown at workThread.Star t().
      <snip>
      Dim workThread As New Thread(New ThreadStart(Add ressOf ftp.doWork))
      <snip>
      For iFile = 0 To myfiles.Count - 1
      >
      workThread.Star t()
      ftp.UploadFile( postedFile.File Name)
      workThread.Join ()
      End If
      >
      Next iFile
      >
      workThread.Star t() above is where the error is thrown after the first
      upload.
      >
      <snip>
      >
      As the error says, the thread has already run, it seems you can't
      'restart' it. I guess you'll have to recreate the thread at each
      cicle.
      >
      Anyway, it seems that there are other issues with your code. I
      couldn't identify the class you're using as "ftp", but it seems that
      you're starting the thread to "doWork" while also commanding the ftp
      object to "UploadFile ". This seems a bit confusing: which one is
      actually performing the download? Maybe you could post more
      information about the kind of classes you're using.
      >
      Regards,
      >
      Branco.

      Thanks for your reply Branco.

      The ftp class is the MSDN-provided class for uploading files to a
      remote server.
      I'll spare you the massive code dump, but if you want to check it out,
      its called
      clsFTP and it is here: http://www.dotnethero.com/hero/vbnet/ftp.aspx?nmx=8_4
      I have instantiated it for local file access as: Dim ftp As New clsFTP

      I have made some changes to clsFTP so I can count bytes as files are
      uploaded.
      Namely, I have added the following event handler:

      Delegate Sub IncrementEventH andler(ByVal sender As Object, ByVal e As
      IncrementEventA rgs)

      -- and --

      Public Event Incremented As IncrementEventH andler

      The event handler gets fired every time the amount of bytes are
      incremented during the upload. I have made this change in the
      UploadFile method 2/3rds the way down clsFTP:

      Do While (m_iBytes 0)

      cSocket.Send(m_ aBuffer, m_iBytes, 0)
      m_iBytes = input.Read(m_aB uffer, 0,
      m_aBuffer.Lengt h)

      m_iBytesTotal += m_iBytes

      doWork() // THIS METHOD FIRES THE EVENT HANDLER

      Loop

      Public Sub doWork()
      RaiseEvent Incremented(Me, New
      IncrementEventA rgs(m_iBytesTot al))
      End Sub

      Finally, I have a method, SendVideo(), in a different file that gets
      called once the queue of videos starts to upload. As each video is
      uploaded the workThread keeps count of the bytes being uploaded to
      show progress. As mentioned, after the first video is uploaded (which
      succeeds) the error is thrown when the second one starts to upload.
      Here is a bit more of that method:

      Public Sub SendVideo()

      Dim ftp As New clsFTP
      Dim myfiles As HttpFileCollect ion

      Dim workThread As New Thread(New ThreadStart(Add ressOf
      ftp.doWork))

      myfiles = HttpContext.Cur rent.Request.Fi les

      ' subscribe a delegate with the event
      AddHandler ftp.Incremented , AddressOf onIncrement

      If (ftp.Login() = True) Then

      Dim iFile As Integer

      For iFile = 0 To myfiles.Count - 1

      Dim postedFile As HttpPostedFile = myfiles(iFile)
      filesize = postedFile.Inpu tStream.Length

      If Not postedFile.File Name.Equals("") Then

      workThread.Star t()
      ftp.UploadFile( postedFile.File Name)
      workThread.Join ()
      End If
      Next iFile

      ftp.CloseConnec tion()

      End If

      [ETC...]

      Per a discussion with a friend, I am told that this is a classic
      Producer/Consumer situation in multithreading. If you agree with this
      (or have another suggestion), I would appreciate it if you could
      provide code examples in addition to conceptual advice as it helps me
      understand what is going on. Of course, if you need more information,
      please let me know.

      Thanks in advance for your attention.
      Peter

      Comment

      Working...