Interacting with threads in C#

Hi 🙂 I'm writing a "simulator" of the London Underground. Basically I'm writing an engine that is able to move trains from station to station, open doors, take passengers, close doors, wait if there's another train in front and so on. And I want all trains to take these decisions at the same time, which lead me to a multithreaded approach. Unfortunately my experience with threads and design patterns involving threads etc is very limited.

Here's how I'm doing it: I created a thread for each line (say Bakerloo). Each line will have multiple trains on it and each train has a thread assigned to it. Each train will need to check that the next station is free before leaving their current station and so on.

Now I have no idea what's the best approach to communicate with those threads. For example say I want to cancel all trains on one line. I would like to be able to do something along the lines of line.CancelAllServices() and then in that method cancel all the train services on that line (train.Cancel() etc).

Basically the code that creates the thread should receive after the thread creation and interface or class that has the Cancel() methods and other methods it can use to interact with the thread it's just created.

So… any advice? What's the best approach for this scenario?

by superdraco via /r/csharp

Leave a Reply