cmd.exe doesn't work unless....

G

Guest

Hi,

I'm relatively new to C# so apopogies in advance if I am missing something
obvious. I am trying to run a command, which I would normally run from the
command line, from within my C# application. Here is my code:

private void FileMonitor_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
string renamed = e.FullPath;

System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
string strCmdLine;

strCmdLine = "/C Dumper.exe \"" + renamed + "\"";

// MessageBox.Show("");

process1.StartInfo.FileName = "CMD.exe";
process1.StartInfo.Arguments = strCmdLine;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process1.StartInfo.CreateNoWindow = true;
process1.Start();

StreamReader myStreamReader = process1.StandardOutput;
string rdfID = myStreamReader.ReadLine();
process1.Close();
}

The command fails to execute. However, if I were to uncomment
"MessageBox.Show("");" then the command executes correctly. I do not want to
pause my application with a message box, however.

Any ideas as to where I am going wrong? Many thanks in advance.
 
S

Shiva

MessageBox.Show() does not return until the user has responded to it. So,
the subsequent lines will not run until the messagebox is dismissied.

Hi,

I'm relatively new to C# so apopogies in advance if I am missing something
obvious. I am trying to run a command, which I would normally run from the
command line, from within my C# application. Here is my code:

private void FileMonitor_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
string renamed = e.FullPath;

System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
string strCmdLine;

strCmdLine = "/C Dumper.exe \"" + renamed + "\"";

// MessageBox.Show("");

process1.StartInfo.FileName = "CMD.exe";
process1.StartInfo.Arguments = strCmdLine;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process1.StartInfo.CreateNoWindow = true;
process1.Start();

StreamReader myStreamReader = process1.StandardOutput;
string rdfID = myStreamReader.ReadLine();
process1.Close();
}

The command fails to execute. However, if I were to uncomment
"MessageBox.Show("");" then the command executes correctly. I do not want to
pause my application with a message box, however.

Any ideas as to where I am going wrong? Many thanks in advance.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

The existence of that line does not imply nothing , unless that you are
trying to access a file (the method name suggest it's a response to a
FileSystemWatcher event) and the file is still being used by another
program. in this escenario the MessageBox has a delay effect while it the
file may get closed by the other application and it works fine.

try to replace the MessageBox for a Thread.Sleep and see what happen

Cheers,
 
G

Guest

Hi Ignacio,

Your suggestioned worked. I replaced the MessageBox by Thread.Sleep(4000)
and I'm now a happy bunny. Thanks so much for your help.

Kind regards,

Omer
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Good to know

Please remember that its just a "patch" and you need to test it in your
real deployment escenario, it does depend of the speed of the system and how
busy it's so it may not work always.

cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top