473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.Start().

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(AddressOf 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.Start()
ftp.UploadFile(postedFile.FileName)
workThread.Join()
End If

Next iFile

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

Thanks....

Feb 28 '07 #1
2 1446
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.

Feb 28 '07 #2
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

Feb 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: ...
7
by: GTi | last post by:
Does anyone have any good code for a self restarting application? Sometimes I need to exit my application and start it again, what is the best method of doing this in a windows form application....
1
by: Adam Atlas | last post by:
What is the best way for a Python process (presumed to be a script run by the interpreter binary, not embedded in some other program) to restart itself? This is what I've been trying: import...
8
by: SAL | last post by:
Hello, I have a web app (asp.net 2.0) that I'm loosing Session variables in. I implemented: Application_End in Global.asax using the following code: Sub Application_End(ByVal sender As Object,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.