socket inside thread

Hi all,

how can I use a socket inside a thread? The goal of the thread would be to retrieve a large amount of data from the web (using https protocol) then process it. The thread action would be set up in a timer that would update the main interface of the application with newly processed data. Is this possible?
When I use the Socket.Get inside a thread, the actual processing of content occurs after the thread is closed (at least when I run it through debugger.

Many thanks,


Why would you need this? Sockets sort of run in their own thread anyway. At least, they work independently of everything else going on in your code. You could still use a timer to periodically check on the data and update a user interface just like you wanted.

Thanks Marc for the reply. I intended to use threads to retrieve and process data from multiple URLs (dedicating each thread to retrieve and process data from each URL) then update the interface. Given that the data retrieved varies in size between the different URLs, there may be different lengths of time the user will have to wait.
Now that I think of it and based on what you said that sockets run in their own threads, I will just create multiple classes of Socket - one for each URL.

That’s what I would suggest. I have an app that works similarly (downloads data from multiple URLs, potentially at the same time) and I created an array of sockets so each URL has their own. Seems to work just fine.

So would I be correct to assume that you have one socket class in your project and then declare it multiple times with Dim statement? I was initially thinking about dragging 3 or 4 Socket class objects into the project (1 for each URL)…but that seems wasteful.


No, you’re much better off making your own subclass of the socket, with your code inside the subclass. Then you can create an array property of the subclass (in the app object, a window, or a module, wherever you want access to this array):

mySocket() as mySocketClass

When you add to this array, you’d add to it with a new statement, like this:

mySocket.append new mySocketClass

This creates a new instance of mySocketClass that’s stored in the array. When you’re finished with this socket and don’t need it any more, you can use the array.remove command to delete it.

You may need to create some way to keep track of which socket is which (in the array). One way is to add your own ID property to the custom subclass. That way if you need to see which socket is which, you can look at its ID value. You could also do this with a dictionary instead of an array and the dictionary’s key would be your socket’s ID.

Thank you very much for your advice and in-depth description, it is very helpful:)

I am using it like this:

for x as Integer = 0 to maxSockets -1 if mySocket(x) <> nil then if not mySocket(x).IsConnected then mySocket(x) = new mySocketClass mySocket(x).Address = "server address" mySocket(x).Port = 80 mySocket(x).Connect end If end if next

This way you can access each socket as mySocket(x) when needed.