I'm trying to understand multi-threading in TCP so I'm coding a basic telnet text "router".
using ReadLine()
each thread using a TCP listener will wait for input from the telnet client and then respond based on the text which is sent. I have this working with multiple threads and multiple telnet clients.
I want to conditionally send messages to all threads.
For example, if the text sent from any one thread is "Alert!" then I want every thread for connected clients to execute WriteLine("Alert!")
Does this make sense?
My problem is that I don't know how to make one thread raise an event in another thread.
You need to look at a Event Broker pattern. Basically you would have one object with an event that all your threads subscribe to. It will also have a method that can be called which will invoke the event. It maybe sounds complicated, but its fairly simple.
Example code is here http://msforge.net/blogs/paki/archive/2007/11/20/EventBroker-implementation-in-C_2300_-full-source-code.aspx.
Don't think of threads as data. Think of them as constructs.
Obviously to have class A raise an event in class B, B must reference A and subscribe to its event.
But honestly, I think you are going about it the wrong way. Have a single TCP listener. When a message comes in, you'll get a web request object and you can process in its own thread. When processing the thread, if you find word "Alert", generate an event to a higher level class. Then handle the event and do whatever needs to be done. Example architecture:
Manager instantiates TcpHandler and subscribes to its AlertReceived event.
TcpHandler instantiates MessageProcessor and subscribes to its MessageReceived event.
When TcpHandler reads something off its TcpListener object, fire off the MessageProcessor class and have it read the actual data on another thread.
Fire the MessageReceived event. Then in the TcpHandler class, handle the event. If the data received is "Alert", fire the AlertReceived event.
The Manager class will catch the event and do whatever else you then desire.