Home >Backend Development >C++ >How to Execute Multiple Commands in a Single .NET Process Without Creating New Processes?
Execute Multiple Commands without Creating New Processes using .NET
To efficiently execute multiple commands without creating a new process for each one, you can redirect standard input and utilize a StreamWriter to write to the standard input. Here's how you can modify your code:
using System.Diagnostics; namespace ExecuteMultipleCommands { class Program { static void Main(string[] args) { Process p = new Process(); ProcessStartInfo info = new ProcessStartInfo(); info.FileName = "cmd.exe"; info.RedirectStandardInput = true; info.UseShellExecute = false; p.StartInfo = info; p.Start(); using (StreamWriter sw = p.StandardInput) { if (sw.BaseStream.CanWrite) { sw.WriteLine("mysql --user=root --password=sa casemanager"); sw.WriteLine("your-query-here"); sw.WriteLine("exit"); } } } } }
This updated code will start the DOS command shell, switch to the MySQL command shell by entering "mysql --user=root --password=sa casemanager", execute your desired MySQL query, and finally exit the MySQL shell by inputting "exit."
By using a StreamWriter to write to the standard input of the shell process, you can effectively execute multiple commands within a single process, streamlining your process execution and improving overall performance.
The above is the detailed content of How to Execute Multiple Commands in a Single .NET Process Without Creating New Processes?. For more information, please follow other related articles on the PHP Chinese website!