Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# launches another program

C# launches another program

Scheduled Pinned Locked Moved C#
helpcsharpjavacareer
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ipstefan
    wrote on last edited by
    #1

    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.

    D D P H 4 Replies Last reply
    0
    • I ipstefan

      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.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • I ipstefan

        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.

        D Offline
        D Offline
        Dragonfly_Lee
        wrote on last edited by
        #3

        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~

        1 Reply Last reply
        0
        • I ipstefan

          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.

          P Offline
          P Offline
          Pritam Karmakar
          wrote on last edited by
          #4

          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 1 Reply Last reply
          0
          • I ipstefan

            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.

            H Offline
            H Offline
            HosamAly
            wrote on last edited by
            #5

            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 modify run.bat to call your class, but you should leave all other commands in the batch file as-is.

            My LinkedIn Profile

            I 1 Reply Last reply
            0
            • P Pritam Karmakar

              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 Offline
              I Offline
              ipstefan
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • H HosamAly

                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 modify run.bat to call your class, but you should leave all other commands in the batch file as-is.

                My LinkedIn Profile

                I Offline
                I Offline
                ipstefan
                wrote on last edited by
                #7

                run.bat launches the java programs in .jar format(tnttolem.jar,towp.jar,txttoint.jar) - also run.bat uses a perl program too.

                H 1 Reply Last reply
                0
                • I ipstefan

                  run.bat launches the java programs in .jar format(tnttolem.jar,towp.jar,txttoint.jar) - also run.bat uses a perl program too.

                  H Offline
                  H Offline
                  HosamAly
                  wrote on last edited by
                  #8

                  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.)

                  My LinkedIn Profile

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups