Answered by:
Trouble threading windows service TCP socket client

Question
-
I'm trying to build a multithreaded SMPP client with VS2008 and I'm getting bogged down in threading. Basically my main parent thread will interact with the windows service scheduler, get registry settings, and get a list of SMPP servers to connect to. Then it will launch a thread for each SMPP connection, which will need to keep it's TCP socket open to receive messages with, so the thread must remain running even if there aren't any events happening (no messages to receive)... but I can't seem to get it figured out.
Here's the basic thread hierarchy I'm shooting for;
- parent thread (windows service)
- SMPP connection threads (could be up to 100 threads/connections)
I'm pulling SMPP channel data from a database into a recordset and launching the child SMPP threads using the following code;
While oRS.Read()
oChannel = New classSMPPChannel 'create new SMPP object
oChannel.ChannelID = oRS("ChannelID").ToString
Threading.ThreadPool.QueueUserWorkItem(AddressOf oChannel.Start, oChannel)
oChannel = Nothing 'kill object
End While
The Start sub in the oChannel object does run, but once that sub is finished running the thread ends. In the Start sub, it calls other code that initiates a connection to the remote SMPP server, but that code asynchronously waits for an event to be raised regarding the connection status. Once the sub finishes, the async connection being attempted doesn't seem to be able to pin the thread down.The bottom line here is that I want each child thread to kind of operate like it's own independent client process. Is that even possible? Do I need to move all my SMPP connection management logic into my main/parent thread and make this a single-threaded application? I really hope not.Thanks in advance!
- Edited by ishmell Sunday, January 22, 2012 8:34 AM
Sunday, January 22, 2012 8:31 AM
Answers
-
Hi,
>>Do I have to put a sleep loop into the called sub? Is that how it's done?
If you want to wait the new thread exit, you can use the Join method: http://msdn.microsoft.com/en-us/library/system.threading.thread.join(v=vs.100).aspx
Have a good day.
Call me ghost for short, Thanks
To get the better anwser, it should be a better question.- Proposed as answer by Mike FengModerator Tuesday, January 31, 2012 6:41 AM
- Marked as answer by Mike FengModerator Friday, February 10, 2012 3:05 AM
Wednesday, January 25, 2012 6:20 AM
All replies
-
Hi Ishmell,
Your issue should be a issue about thread pool, rather than TCP socket client.
And there are a lot of examples about thread pool: http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=threadpool+VB+site%3Ahttp%3A%2F%2Fwww.codeproject.com
Enjoy your days.
Call me ghost for short, Thanks
To get the better anwser, it should be a better question.Wednesday, January 25, 2012 3:20 AM -
Hi Ishmell,
Your issue should be a issue about thread pool, rather than TCP socket client.
And there are a lot of examples about thread pool: http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=threadpool+VB+site%3Ahttp%3A%2F%2Fwww.codeproject.com
Enjoy your days.
Call me ghost for short, Thanks
To get the better anwser, it should be a better question.Thanks for your response CrazyGhost_Von, but my main question is how to keep threads running once the called sub ends. Do I have to put a sleep loop into the called sub? Is that how it's done?
The problem is this - when I create a new thread to handle connecting to a remote SMPP server, I create the thread, pass it an object called oChannel, and start the thread processing oChannel.Start. In the Start sub, how to I keep the thread running after connecting to the remote SMPP server via TCP? Do I then enter a sleep loop and wait for events to fire? Is that the "best practices" way it's done?
Wednesday, January 25, 2012 3:36 AM -
Hi,
>>Do I have to put a sleep loop into the called sub? Is that how it's done?
If you want to wait the new thread exit, you can use the Join method: http://msdn.microsoft.com/en-us/library/system.threading.thread.join(v=vs.100).aspx
Have a good day.
Call me ghost for short, Thanks
To get the better anwser, it should be a better question.- Proposed as answer by Mike FengModerator Tuesday, January 31, 2012 6:41 AM
- Marked as answer by Mike FengModerator Friday, February 10, 2012 3:05 AM
Wednesday, January 25, 2012 6:20 AM