Start function

  • Thread starter Thread starter Alan T
  • Start date Start date
A

Alan T

I have a command line:

myUtility.exe -1 -x myUtility.xml C:\Temp\MyDoc.doc > C:\Temp\MyDoc.txt



It works fine in the command prompt (ie. DOS prompt), it converted a doc
file into test file.



However, if I run in C#:

System.Diagnostics.Process.Start("myUtility.exe", "-1 -x myUtility.xml " +
@"C:\Temp\MyDoc.doc" + " >" + @"C:\Temp\MyDoc.txt");

That failed to created a output text file.

Any idea?



I put the myUtility.exe into my C# executable directory, so it is able to
find this and also the xml file. In addition, there is no exception raised
so I think it is executed successfully.
 
System.Diagnostics.Process.Start("myUtility.exe", "-1 -x myUtility.xml
" +
@"C:\Temp\MyDoc.doc" +
" >" + @"C:\Temp\MyDoc.txt");
^
There isn't a space between " >" and "C:\Temp\MyDoc.txt"
it should be " > " + @"C:\Temp\MyDoc.txt");
 
That's my typo.
It has a space in front of and behind the ">" in my source code but still
does not work.
 
Alan said:
I have a command line:

myUtility.exe -1 -x myUtility.xml C:\Temp\MyDoc.doc > C:\Temp\MyDoc.txt

It works fine in the command prompt (ie. DOS prompt), it converted a doc
file into test file.

However, if I run in C#:

System.Diagnostics.Process.Start("myUtility.exe", "-1 -x myUtility.xml " +
@"C:\Temp\MyDoc.doc" + " >" + @"C:\Temp\MyDoc.txt");

That failed to created a output text file.

Try:

System.Diagnostics.Process.Start("cmd", "/c myUtility.exe -1 -x
myUtility.xml " +
@"C:\Temp\MyDoc.doc" + " >" + @"C:\Temp\MyDoc.txt");


ARne
 
Hi,

It works as followed your format.
What is the problem in my original version?

And what is the '/c' for ?
 
It works as followed your format.
What is the problem in my original version?

And what is the '/c' for ?

I belive it is cmd that supports the > redirection.

/c is something that cmd needs - we always use it. Try
read:
cmd /?

Arne
 
I got a problem is I will execute this start procedure a number of times in
a loop, say 100 times,
it pops up 100 times the command windows so how do I make it invisible ?
 
Alan said:
I got a problem is I will execute this start procedure a number of times in
a loop, say 100 times,
it pops up 100 times the command windows so how do I make it invisible ?

There are an overload of Start that takes a
ProcessStartInfo as argument and that has a
WindowsStyle property that can be
ProcessWindowStyle.Hidden.

Which sounds as what you want.

Arne
 
Hi,

It works for me, thanks.
Now I got another problem is in my Start, that is the timing issue.

In my Start process, I am going to export data into a text file, so I need
to make sure the exported file is created and the process is finished before
I access the text file. I got an exception when I tried to access the text
file before the export is finished.

How do I know the export is finished ?
 
Alan said:
Now I got another problem is in my Start, that is the timing issue.

In my Start process, I am going to export data into a text file, so I need
to make sure the exported file is created and the process is finished before
I access the text file. I got an exception when I tried to access the text
file before the export is finished.

How do I know the export is finished ?

Process has WaitForExit method.

Arne
 
What should I call after

try
.....
.....
myProcess.WaitForExit();
while (!myProcess.HasExited)
{
Thread.Sleep(1000);
}
//myProcesss.Dispose();
//myProcess.Close();
except
try
//myProcesss.Dispose();
//myProcess.Close();
except
;
end;
end;
 
Alan said:
What should I call after

try
.....
.....
myProcess.WaitForExit();
while (!myProcess.HasExited)
{
Thread.Sleep(1000);
}
//myProcesss.Dispose();
//myProcess.Close();
except
try
//myProcesss.Dispose();
//myProcess.Close();
except
;
end;
end;

I can not see the purpose of testing for HasExited
when calling WaitForExit.

Arne
 
Now I can use the myProcess.WaitForExit() method.
My current problem is when the .exe in the myProcess is exporting data
(extract text) from one file to another file, it hangs at some files.

eg. myExe.exe file1.doc > file2.txt

I tried in cmd, it stays in the command prompt and the output file is empty,
ie 0 KB.
The memory usage in the Task Manager shows nearly my virtual memory.

I think myExe.exe cannot handle the file1.

This is the source code:

try
Process myProcess = new Process();
myProcess.StartInfo.Filename = "cmd";
myProcess.StartInfo.Arguments = "myExe.exe file1.doc > file2.txt";
myProcess.StartInfo.CreateNoWindows = true;
myProcess.StartInfo.WindowStyle = ProcessWindowsStyle.Hidden;
myProcess.Start();
myProcess.WaitForExit(5000);
myProcesss.Dispose();
except
try
myProcesss.Dispose();
except
;
end;
end;

This is what I do, just wait for 5 seconds and kills the process.
But, I found the myExe.exe still shown in the Task Manager and eats up
memory. My machine nearly hang.
 
Alan said:
Now I can use the myProcess.WaitForExit() method.
My current problem is when the .exe in the myProcess is exporting data
(extract text) from one file to another file, it hangs at some files.

eg. myExe.exe file1.doc > file2.txt

I tried in cmd, it stays in the command prompt and the output file is empty,
ie 0 KB.
The memory usage in the Task Manager shows nearly my virtual memory.

I think myExe.exe cannot handle the file1.
This is what I do, just wait for 5 seconds and kills the process.
But, I found the myExe.exe still shown in the Task Manager and eats up
memory. My machine nearly hang.

I really think that fixing myexe.exe is the best solution for
this problem.

Arne
 
Back
Top