Server Multithreading Help
Posted: Tue Nov 29, 2011 11:07 am
Right I have a extremely frustrating error that I have never been able to fix.
I am using C#.Net and the bog standard System.Threading namespace and I it's fine if you give me code for VB.Net or .Net for C++.
I am trying to get a multi-threading system going in the web server so it can process more than 1 request at the same time (different environment). Every connection to the server is placed in an array (which then gets removed when it disconnects). When it comes to actually receiving from the socket, that part is in a seperate thread. I pass the index of the entry that the connection is in (with the socket) to the thread (it's a ParameterizedThreadStart).
I set the apartment state of the thread to MTA and then start it.
This is my problem, When I request a page (with images) from the server, the server sends the data to the wrong socket (as if the index is coming from the next index in the loop.
I have tried seting the apartment state to STA, and even made a bool outside the thread that is set to true when the thread has grabbed the socket from the collection, but still to no success. The server then hangs until the that bool is set
I do not know what else to try, I tried google but there was no helpful information but there where recommendations to use a seperate library, but don't really like the idea (my own preference really).
I really don't want to scrap the server requests from being non-threaded because it will slow page load times right down
I am using C#.Net and the bog standard System.Threading namespace and I it's fine if you give me code for VB.Net or .Net for C++.
I am trying to get a multi-threading system going in the web server so it can process more than 1 request at the same time (different environment). Every connection to the server is placed in an array (which then gets removed when it disconnects). When it comes to actually receiving from the socket, that part is in a seperate thread. I pass the index of the entry that the connection is in (with the socket) to the thread (it's a ParameterizedThreadStart).
I set the apartment state of the thread to MTA and then start it.
This is my problem, When I request a page (with images) from the server, the server sends the data to the wrong socket (as if the index is coming from the next index in the loop.
I have tried seting the apartment state to STA, and even made a bool outside the thread that is set to true when the thread has grabbed the socket from the collection, but still to no success. The server then hangs until the that bool is set
I do not know what else to try, I tried google but there was no helpful information but there where recommendations to use a seperate library, but don't really like the idea (my own preference really).
I really don't want to scrap the server requests from being non-threaded because it will slow page load times right down