I don’t know much about threads, but I have a function in the main class of my console application called SendProcessCmd(string cmd). In my main() I create a process, and store the StreamWriter as a class member var, and my SendProcessCmd() issues the .WriteLine() commands to it.
I have another thread with a TCP server that listens for connections, then allows these to send commands to the process using Program.SendProcessCmd(). Is it safe to do this?
The safest method I can think of would be to find the running process in my server’s thread, create a new StreamWriter, then issue the commands. However, this seems like a rather long way around to do the same thing.
The whole purpose of threads is that you can always call any function and access any data structure from any thread. There are gotcha’s, though: