Hyperion
Hyperion

Reputation: 909

Running multiple commands through cmd.exe using C#

I am trying to run multiple commands in one process instance and what I have is working but it hangs and does nothing after the 7th or 8th command in the streamwriter.

Here is my code:

Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.CreateNoWindow = true;
p.StartInfo = info;
p.Start();

using (StreamWriter sw = p.StandardInput)
{
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\a*.sql a_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\b*.sql b_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\c*.sql c_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\d*.sql d_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\e*.sql e_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\f*.sql f_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\g*.sql g_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\i*.sql i_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\l*.sql l_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\m*.sql m_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\n*.sql n_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\o*.sql o_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\p*.sql p_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\q*.sql q_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\r*.sql r_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\s*.sql s_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\t*.sql t_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\v*.sql v_updates.sql");
   sw.WriteLine("copy /b updates\\EDB-master\\tables\\w*.sql w_updates.sql");
}
p.WaitForExit();

I know I can accomplish writing all the SQL files into only a single file, but the reason I'm just going by first letter is because it's too large of a packet size for default MySQL servers with one large sql import. (program is for a public release and would hate to make everyone edit their max_packet_size just to use the app)

So what I basically need help with is figuring out why it only executes part of the commands or if there is another way I can do this

Upvotes: 0

Views: 1508

Answers (2)

Hyperion
Hyperion

Reputation: 909

Got this answer from another post, Programmatic use of cmd.exe from C#

Running the cmd in it's own console worked.

Upvotes: 0

LB2
LB2

Reputation: 4860

I see you redirecting both, StdIn and StdOut, but once you start the process, you only write to StdIn, but don't provide any sink for StdOut redirection. So what probably happens is that child process is riting, nothin is reading, so it blocks when StdOut is full, and you got a deadlock. Try setting RedirectStandardOutput=false and that should do it.

Upvotes: 1

Related Questions