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.Start().
<snip>
Dim workThread As New Thread(New ThreadStart(AddressOf ftp.doWork))
<snip>
For iFile = 0 To myfiles.Count - 1
workThread.Start()
ftp.UploadFile(postedFile.FileName)
workThread.Join()
End If
Next iFile
workThread.Start() 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 IncrementEventHandler(ByVal sender As Object, ByVal e As
IncrementEventArgs)
-- and --
Public Event Incremented As IncrementEventHandler
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_aBuffer, 0,
m_aBuffer.Length)
m_iBytesTotal += m_iBytes
doWork() // THIS METHOD FIRES THE EVENT HANDLER
Loop
Public Sub doWork()
RaiseEvent Incremented(Me, New
IncrementEventArgs(m_iBytesTotal))
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 HttpFileCollection
Dim workThread As New Thread(New ThreadStart(AddressOf
ftp.doWork))
myfiles = HttpContext.Current.Request.Files
' 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.InputStream.Length
If Not postedFile.FileName.Equals("") Then
workThread.Start()
ftp.UploadFile(postedFile.FileName)
workThread.Join()
End If
Next iFile
ftp.CloseConnection()
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