A quick introduction to what I'm trying to do. I want to start the process and start two threads to control stderr and stdin. Each stream destroys the bit of the stream and then launches it in NetworkStream. If there is an error in any thread, both threads must die immediately.
Each of these processes, with the stdout and stdin monitoring flows, is allocated by the main server process. The reason this gets complicated is that at any given time, 40 or 50 of these processes can easily be. Only during the morning bursts of a reboot, there are more than 50 connections, but in fact it should handle 100 or more. I am testing 100 concurrent connections.
try { StreamReader reader = this.myProcess.StandardOutput; char[] buffer = new char[4096]; byte[] data; int read; while (reader.Peek() > -1 ) // This can block before stream is streamed to { read = reader.Read(buffer, 0, 4096); data = Server.ClientEncoding.GetBytes(buffer, 0, read); this.clientStream.Write(data, 0, data.Length); //ClientStream is a NetworkStream } } catch (Exception err) { Utilities.ConsoleOut(string.Format("StdOut err for client {0} -- {1}", this.clientID, err)); this.ShutdownClient(true); }
This code block runs in a single thread, which is not Background now. A similar stream for a StandardError stream. I use this method instead of listening to OutputDataReceived and ErrorDataReceived because there was a problem in Mono that caused these events to not always fire properly, and although they seem to be fixed, I like that this method ensures that I I read and write everything in sequence.
ShutdownClient with True is simply trying to kill both threads. Unfortunately, the only way I found this work is to use an interrupt for the stdErrThread and stdOutThread objects. Ideally, peek will not block, and I could just use the manual reset event to continue checking for new data on stdOut or stdIn, and then just die when the event is flipped.
I doubt this is the best way to do this. Is there a way to accomplish this without using an interrupt?
I would like to change because I just saw in my logs that I missed the ThreadInterruptException inside Utlities.ConsoleOut. It just does System.Console.Write if the static variable is true, but I assume it blocks somewhere.
Changes:
These threads are part of the parent thread, which is started massively by the server upon request. Therefore, I cannot install the StdOut and StdErr threads in the background and kill the application. I could kill the parent thread from the main server, but this will become sticky again with Peek locking.
Added information about this as a server.
I'm also starting to realize that the best Queuing method for queries can be the ultimate solution.