VB.Net Explanation on Multithreading needed

Hi Experts.  

In VB.Net, I have a process in a single sub that carries out certain checks and then sends messages.  In carrying out the checks it calls a socket.Receive function to test the availability of a server.

This works well but takes about 3 to 5 seconds to run and could run up to 2000 times.   Due to the time, I changed things so that the sub is called on different threads, with this code

 Dim NewThread As New System.Threading.Thread(AddressOf StartTheProcess)

This works well if the sub is not called to quickly, once every second seems OK.  But faster than this I get errors relating to the socket function.  

My question is - if the sub is running multiple times, will there be multiple calls to the same socket function at the same time or is it separated in some way?

In general, in multithreading, if there is a function called from different threads, do they interact with the same function.

If this is the case, what is the way round it?

Thanks for any help.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Well, if you call a function, you call a function. So yes if you call it from different threads, it will be called many times.

There are many possible reasons that can cause things to go bad in multithreading. A frequent one is the following.

Multithread involves the system switching continually between the different threads. It is quite possible that one call starts the function, then the system switch to another thread before the function as terminated its job. If the function locked a resource and the switch occured before that resource was freed, the call from the second thread might bomb because the resource is locked.

Not all functions are thread-safe. You need to check the documentation. You might also see if there is not an asynchrone equivalent of that function. Asynchrone functions usually automatically works on a their own thread and have been designed to handle the type of problem I described. Typically, an Async function will have the same name as the original function with Async as a suffix (GetData and GetDataAsync). But there might also be an alternative that has a complete different name. If you have some, check the documentation for the class that contains your function.
PNRTAuthor Commented:
Many Thanks James
What if I was to have the new thread open a further thread on which the same functionality as the function could be handled.  I would think that this would be kept separate for each thread?
Jacques Bourgeois (James Burger)PresidentCommented:
Don't go overboard with threads. They are very useful, but can become an horror if you go to far and try to correct a problem that you do not understand by adding still more threads over the thing.

If the problem is something like what I told you (and it could be something else), there is nothing you can do. If a file is locked by one thread, it is locked for all the others, because in the background, they all use the same file. Many functions cannot be called from a second thread. Go through the .NET documentation, and you will find many functions that are documented as not being thread safe. You will usually have problems whatever you do when using these function in a multithread environment.

I am talking from my general experience with multi-threading. I have no experience working with sockets, so I cannot address your specific problem. But the first thing I would do if I was in your situation would be to look at the documentation.

So I put myself in your shoes and went for the Socket class documentation. First thing first, give a look at the Socket constructor. And right there, I had the following in the Remarks section of the page:

If you call the Socket constructor multiple times with the same byte array as the argument for each call, you will create multiple managed Sockets with the same underlying socket. This practice is strongly discouraged. That might be your problem. If not, you have to look at all the methods and properties that your function use to make sure that they are safe to use in a parallel environment.

Multithreading is not a solution for everything. You know, processors still do only one thing at a time. So when using multiple threads, you are not performing multiple operations at the same time. The processor simply gives little slots of time to each thread, switching from one to the other after each little slot. The work required to control the thing and switch from one thread to another actually slows down the process. Most of the time, operations performed in multiple threads will take up more time to run than the same operations performed sequentially.

Multithreading is useful when there is a user interface, because it lets the user works at his very slow pace while the extra threads "seem" to run in the background. Its not designed to solve performance problems, only perceived performance problems. By using multiple threads, you just make things slower, more complex and run the risk of getting into problems such as yours.

To solve your performance problem, I suggest that you start another "thread", asking a question that deals with sockets instead of multithreading. This will attract answers from experts who work often with sockets. Post some code. They might know different ways to work with sockets than what you do, and that can be faster in your specific situation.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PNRTAuthor Commented:
Thanks James
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.