Please explain the process termination mechanism in the c # winforms application. In my application, there is a loop that runs cmd to execute a command. Depending on the launch parameters, the command may work at different times, therefore it is necessary to give time to wait for completion.
So far I have done this:
Process vote = new Process(); vote.StartInfo = new ProcessStartInfo(@"cmd"); vote.StartInfo.RedirectStandardOutput = true; vote.StartInfo.RedirectStandardInput = true; vote.StartInfo.UseShellExecute = false; vote.StartInfo.CreateNoWindow = true; vote.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; vote.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler); vote.Start(); vote.StandardInput.WriteLine(vote_string); vote.BeginOutputReadLine(); vote.WaitForExit(7000); vote.Close();
At the same time, I notice in the task manager that sometimes there are too many running cmd, which clogs the RAM and leads to a performance drop.
I began to study msdn on the topic of stopping processes and still did not understand how to organize the release of resources in my case.
In essence, the questions are as follows:
Is cmd shutting down on successful completion? seemingly not
how to use waitforexit correctly? after all, it does not imply the release of resources, but only indicates whether the process is completed or not. Apparently, it is necessary to write an if-block, which, in the case of a negative answer from waitforexit, will forcibly close the process, is that so?
What method to use to complete the process? about close () it says that it frees up resources. Does the process terminate or do you still need to use kill ()?