C# launches another program
-
I have a program written in C# and from it I want to start another file ,some run.batch (which has a java program written behind) . Problem is that the 2nd program(a text processor) has an error when it is processing a text in 0:00:00 (no time) . But it works. I just have to click cancel debugging, so I can do my job further. 2nd program is closed source and cannot be modified. What I want to do is to catch the errors from my main program if possible. Thank you.
-
I have a program written in C# and from it I want to start another file ,some run.batch (which has a java program written behind) . Problem is that the 2nd program(a text processor) has an error when it is processing a text in 0:00:00 (no time) . But it works. I just have to click cancel debugging, so I can do my job further. 2nd program is closed source and cannot be modified. What I want to do is to catch the errors from my main program if possible. Thank you.
There only way you could do that would be to constantly poll the windows, looking for a window with the proper title. This requires enumerating the open windows using FindWindow[^], EnumChildWindows[^] But, you have a bigger problem. Java windows do not use standard Windows controls, so you really can't send keystrokes to them. The best you can do is send keystrokes to the parent Windows window and hope for the best.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
I have a program written in C# and from it I want to start another file ,some run.batch (which has a java program written behind) . Problem is that the 2nd program(a text processor) has an error when it is processing a text in 0:00:00 (no time) . But it works. I just have to click cancel debugging, so I can do my job further. 2nd program is closed source and cannot be modified. What I want to do is to catch the errors from my main program if possible. Thank you.
ipstefan wrote:
What I want to do is to catch the errors from my main program if possible.
So far, we can not do this since no way to catch the exception from other process. We can redirect the error output and check the error code. Nevertheless, this is not what you want. :)
I Love KongFu~
-
I have a program written in C# and from it I want to start another file ,some run.batch (which has a java program written behind) . Problem is that the 2nd program(a text processor) has an error when it is processing a text in 0:00:00 (no time) . But it works. I just have to click cancel debugging, so I can do my job further. 2nd program is closed source and cannot be modified. What I want to do is to catch the errors from my main program if possible. Thank you.
you can use following code to run any extrenal file( batch/exe)
Process p = new Process();
p.StartInfo.FileName = pathToCommandLineTool;
p.StartInfo.Arguments = commandLineInputParameters;
p.Start();
p.WaitForExit();
p.Close() -
I have a program written in C# and from it I want to start another file ,some run.batch (which has a java program written behind) . Problem is that the 2nd program(a text processor) has an error when it is processing a text in 0:00:00 (no time) . But it works. I just have to click cancel debugging, so I can do my job further. 2nd program is closed source and cannot be modified. What I want to do is to catch the errors from my main program if possible. Thank you.
Could you wrap the second program into another one? For example:
public class YourJavaClass {
public static void main(String[] args) {
try {
YourSecondAppMainClass.main(args);
} catch (Throwable t) {
// do whatever you want with the exception
}
}
}This isn't exactly catching the exception from C#, but this is where you can begin. You have the exception information, so you may print something predetermined to
System.err
and handle it in your C# application. Note that you'll have to modifyrun.bat
to call your class, but you should leave all other commands in the batch file as-is. -
you can use following code to run any extrenal file( batch/exe)
Process p = new Process();
p.StartInfo.FileName = pathToCommandLineTool;
p.StartInfo.Arguments = commandLineInputParameters;
p.Start();
p.WaitForExit();
p.Close()man...I already use this. string targetDir; targetDir = string.Format(@"E:\desk\baaaars\copy\copy\romm"); p = new Process(); p.StartInfo.WorkingDirectory = targetDir; p.StartInfo.FileName = "run.bat"; // p.StartInfo.Arguments = string.Format("C-Sharp Console application"); p.StartInfo.CreateNoWindow = false; p.Start(); p.WaitForExit(); read the content not just the title next time.
-
Could you wrap the second program into another one? For example:
public class YourJavaClass {
public static void main(String[] args) {
try {
YourSecondAppMainClass.main(args);
} catch (Throwable t) {
// do whatever you want with the exception
}
}
}This isn't exactly catching the exception from C#, but this is where you can begin. You have the exception information, so you may print something predetermined to
System.err
and handle it in your C# application. Note that you'll have to modifyrun.bat
to call your class, but you should leave all other commands in the batch file as-is. -
run.bat launches the java programs in .jar format(tnttolem.jar,towp.jar,txttoint.jar) - also run.bat uses a perl program too.
This shouldn't be a problem. You'll just need to add the three JAR files to the classpath, and proceed as I explained previously. If you don't know what's the main method, check the MANIFEST.MF file inside the main JAR, where you'll find it mentioned. If not, you'll find it somewhere in the batch file, but you may have to dig deeper. (You could post the batch file if you want, and I'll try to help you with it.)