0

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

Hyperion
  • 869
  • 1
  • 8
  • 23
  • 2
    Just a suggestion, have you considered using [File.Copy](http://msdn.microsoft.com/en-us/library/system.io.file.copy(v=vs.110).aspx) instead? – John Gibb Feb 19 '14 at 18:50
  • @JohnGibb, the comment you deleted is what worked for me. Running it in it's own console application. Thank you. (No I have not tried File.Copy instead, it's not really the route I want to go) – Hyperion Feb 19 '14 at 19:04

2 Answers2

1

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.

LB2
  • 4,802
  • 19
  • 35
  • Well that was progress thank you, but now it stops at command 13 and doesn't continue – Hyperion Feb 19 '14 at 18:51
  • Doh! Also set `RedirectStandardError` to false. I.e. only redirect those streams which you plan to handle yourself, all others leave as is. – LB2 Feb 19 '14 at 18:53
  • I just done a small program couple of weeks ago that only redirected StdOut, and runs with no problems... but StdOut, not StdIn... Maybe StdIn is different, but I'd be surprised if that's the case. I'm pretty sure StdErr is the second culprit in the code there. – LB2 Feb 19 '14 at 19:00
  • Yea I bet you're right. I'm going to delete my former comment to avoid confusing people in the future! – John Gibb Feb 19 '14 at 19:01
0

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

Running the cmd in it's own console worked.

Community
  • 1
  • 1
Hyperion
  • 869
  • 1
  • 8
  • 23