-
Oct 15th, 2014, 08:08 AM
#1
Thread Starter
Junior Member
Stream unreadable
Ok, so I have a for loop that runs multiple threads that calls a sub each time and in this sub I make a new stream_reader, the program runs fine running on the normal thread but as soon as I make more than 1 it crashes and says "stream not readable" on line
Read_Stream = New StreamReader(m_sslStream)
Normally my program doesn't have a problem on the main thread, it can go through the for loop and create a new readstream everytime, but soon as I enable threads, broken.
-
Oct 15th, 2014, 08:16 AM
#2
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Oct 15th, 2014, 08:17 AM
#3
Re: Stream unreadable
We'll probably have to see a larger chunk of your code to see what the problem is. Can you post it please? Please use [CODE] tags around it to keep it nice and tidy as well.
It sounds like you might have a cross-thread access problem though.
-
Oct 15th, 2014, 08:42 AM
#4
Thread Starter
Junior Member
Re: Stream unreadable
Originally Posted by Jenner
We'll probably have to see a larger chunk of your code to see what the problem is. Can you post it please? Please use [CODE] tags around it to keep it nice and tidy as well.
It sounds like you might have a cross-thread access problem though.
I thought this but then I thought.. it's on a new thread so it's completely different from it? so how can it be a problem?
-
Oct 15th, 2014, 08:47 AM
#5
Thread Starter
Junior Member
Re: Stream unreadable
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For x As Integer = 1 To CInt(nudThreads.Value)
Dim t As Threading.Thread = New Threading.Thread(Sub() Getinbox())
t.Start()
Next
in my getinbox code
Code:
NetworkS_tream = POP3.GetStream()
m_sslStream = New SslStream(NetworkS_tream)
m_sslStream.AuthenticateAsClient(PopHost)
Read_Stream = New StreamReader(m_sslStream)
-
Oct 15th, 2014, 09:00 AM
#6
Re: Stream unreadable
Do we have to guess what type of object POP3 is, or can you tell us? The name only suggests what it does.
The educated guess answer is that POP3.GetStream returns its single instance internal stream, and you are basically trying to access this same stream on multiple threads.
Think of it like loads of different people trying to write on the same piece of paper at the same time, where should each one start to ensure that people are not destroying each others work.
-
Oct 15th, 2014, 09:03 AM
#7
Re: Stream unreadable
A quick google shows your code to be nearly identical to this link. This shows POP3 as TCPClient, which will only give you a single stream, if you want multiple streams, you have to create multiple connections.
-
Oct 15th, 2014, 09:52 AM
#8
Thread Starter
Junior Member
Re: Stream unreadable
Originally Posted by Grimfort
A quick google shows your code to be nearly identical to this link. This shows POP3 as TCPClient, which will only give you a single stream, if you want multiple streams, you have to create multiple connections.
Yep you are right..
Dim POP3 As New TcpClient is already in my loop though, so it's creating a new one everytime right? no?
Last edited by x34cha; Oct 15th, 2014 at 09:58 AM.
-
Oct 15th, 2014, 10:41 AM
#9
Re: Stream unreadable
I would think No. You have only one reference, POP3, so it can only reference one object at a time. If you set POP3 to reference a new TcpClient in a loop, you lost your reference to the previous TcpClient it reference.
Normally if you want multiple TcpClients you have to have multiple variables to hold references to the object, or more likely use an array or some sort of collection/list to hold the references to the objects.
I would thing the same would be true for your threads. Using the same t variable to hold the reference temporarily to each thread as you start it up, then "toss" it, is not something I would have tried, but perhaps you can "orphan" a thread and it will continue without your program having a reference to it, but it strikes me as being unnatural.
-
Oct 15th, 2014, 10:58 AM
#10
Thread Starter
Junior Member
Re: Stream unreadable
Ok, I will try using an array to hold them, thanks.
-
Oct 15th, 2014, 11:32 AM
#11
Re: Stream unreadable
I would think that having orphaned threads would lead to a memory leak or premature thread termination or some other sort of undesirable outcome.
I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
Oct 15th, 2014, 12:31 PM
#12
Thread Starter
Junior Member
Re: Stream unreadable
Originally Posted by dolot
I would think that having orphaned threads would lead to a memory leak or premature thread termination or some other sort of undesirable outcome.
One step at a time xD
-
Oct 15th, 2014, 02:14 PM
#13
Re: Stream unreadable
I understand.
I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|