C # use Process to call batch processing for blocking issues,
PS: I saw the familiar C # code again. I'm so happy, haha. This time I jumped into the trap and got up again. The company has its own Submit, Compile, and Publish lifecycle systems. During Compile, an external compilation program must be called to Compile the source code. Android's buddy recently changed the packaging tool from ant to gradle, so the system needs to support gradle compilation. The configuration and principles of gradle will not be discussed. You can simply google it. Write a batch processing with parameters to call gradle for compilation and packaging. Then, use process in C # For associated calls. This is a background call and requires compilation of output and error. Of course, redirection of output and error is required. (When looking for batch processing problems, you can discard redirection first so that the results can be output directly in the console)At this time, there was a very confusing problem. The batch processing of associated calls was inexplicably blocked. After the process times out and continues to be executed, the batch processing will continue.When debugging, an error message similar to overflow is displayed when the standoutput attribute is accessed when the output information is read after the timeout. As a result, google found that the batch processing was blocked only when it learned about the output redirection, which is related to this error. First, let me explain my understanding of output redirection. If process sets output redirection, an output buffer is set to write the output results of the program associated with process to the buffer. Process clears the buffer by constantly reading the content of the buffer. The buffer space is limited. If the process does not read data, the buffer will be filled at a certain time. Then, the associated process waits for the data in the buffer to be cleared, and the process waits for the end of the associated process. So,Deadlock occurred. Solution: After process is started, read the output buffer because the reading method is blocked until the associated process ends. To ensure that the main thread is not blocked, the buffer content is read asynchronously by the thread. Note: The thread must be started after the Start method and before the WaitForExit method. In addition, there are two redirection operations for output, output and error, both of which have similar problems. please ensure that the processing is complete.