473,587 Members | 2,547 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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....

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

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.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

Feb 28 '07 #3

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

Similar topics

6
4099
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: Connect Receive response Send Connect ACK
7
26721
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. The reason why I want to this is complicated, so a restart is faster and more easier whey of doing it.
1
6741
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 __main__ for path in sys.path: path += '/' + __main__.__file__ if os.access(path, os.F_OK): break else:
8
2864
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, ByVal e As EventArgs) ' Code that runs on application shutdown Dim b As Boolean Try b =...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8347
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.