none
Not able to launch application on second time without arguments if arguments passed is wrong RRS feed

  • Question

  • I have created a WinFrom application WindowsFormsApp2 as follows

    static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string [] args)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "Launch")
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new Form1());
                    }
                    else if(args[i] == "Exception")
                    {
                        throw new Exception("Launch failed");
                    }
                    else
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new Form2());
                    }
                }
            }
        }

    To launch this application from WindowsFormsApp3 app I have written the code as below

            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    using (Process process = new Process())
                    {
                        process.StartInfo.FileName = @"C:\Users\MYNAME\source\repos\WindowsFormsApp3\WindowsFormsApp3\bin\Debug\WindowsFormsApp3.exe";
                        process.StartInfo.Arguments = @"Exception";
                        if (process.Start())
                        {
                        }
                        else
                        {
                            process.StartInfo.Arguments = string.Empty;
                            process.Start();
                        }
                    }
                }
                catch (Exception ex)
                {
                    using (Process process = new Process())
                    {
                        process.StartInfo.FileName = @"C:\Users\MYNAME\source\repos\WindowsFormsApp3\WindowsFormsApp3\bin\Debug\WindowsFormsApp3.exe";
                        process.StartInfo.Arguments = @"Exception";
                        if (process.Start())
                        {
                 
                        }
                        else
                        {
                            process.StartInfo.Arguments = string.Empty;
                            process.Start();
                        }
                    }
                
                }
            }

    What I want is if the argument passes throws exception I need to relaunch the application without arguments.

    But here always process.Start() returns true;

    Thanks in advance!!..



    • Edited by Joy123456 Tuesday, February 26, 2019 12:39 PM
    Sunday, February 24, 2019 3:14 AM

Answers

  • The task as you have described it is simply impossible.  The ONLY way to get status from another application is to use the exit code, and there are no universal rules on how exit codes are set.  Some applications return 0 on success and non-zero for failure.  Some return 1 for success and 0 for failure.  Many applications don't return anything at all.

    For specific applications, you can find what exit codes it returns by experimenting, by running the app with your examples and printing the ExitCode, but there is nothing you can do in the general case.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Joy123456 Tuesday, March 26, 2019 7:57 AM
    Monday, February 25, 2019 7:18 AM

All replies

  • Exceptions don't cross process boundaries.  As far as Process.Start is concerned, your process DID start correctly.

    If you need to return a status from one process to another, change the Main to return "int" instead of "void", and return 0 on success and some other value on return.  Then, in the calling process, you can get the exit code using process.ExitCode.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Sunday, February 24, 2019 7:36 AM
  • Hi Joy123456,

    Thank you for posting here.

    For your question, I test the code you provided.

    I want to confirm something with you.

    >>To launch this application from WindowsFormsApp3 app I have written the code as below

    1. According to your description, you want to launch WinformFormsApp2 from WindowsForApp3. You need to change the process file name.

    Change WindowsForApp3.exe to WindowsForApp2.exe.

    2. When the Arguments is "Exception", Form2 of WindowsFormApp2 throw new Exception("Launch failed"). 

    The code works well, but it would not show the exception. Please thrown exception with MessageBox.

      MessageBox.Show("Launch failed");

    Here is the code for your reference.

    WindowsFormApp2 Program.cs

     static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                //Application.EnableVisualStyles();
                //Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new Form1());
    
    
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "Launch")
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new Form1());
                    }
                    else if (args[i] == "Exception")
                    {
                        MessageBox.Show("Launch failed");
                    }
                    else
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new Form2());
                    }
                }
            }

    WindowsFormApp3

     private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    using (Process process = new Process())
                    {
                        process.StartInfo.FileName = @"WindowsFormsApp2.exe";
                        process.StartInfo.Arguments = @"Exception";
                        if (process.Start())
                        {
                        }
                        else
                        {
                            process.StartInfo.Arguments = string.Empty;
                            process.Start();
                        }
                    }
                }
                catch (Exception)
                {
                    using (Process process = new Process())
                    {
                        process.StartInfo.FileName = @"WindowsFormsApp2.exe";
                        process.StartInfo.Arguments = @"Exception";
                        if (process.Start())
                        {
    
                        }
                        else
                        {
                            process.StartInfo.Arguments = string.Empty;
                            process.Start();
                        }
                    }
                }

    Here is my result.

    When the Arguments is Exception, it would thrown a exception.

    When the Arguments is "Launch", it would run the Form1 of WindowsFormApp2.

    process.StartInfo.Arguments = @"Launch";

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 25, 2019 3:13 AM
    Moderator
  • Thanks for that.

    But I did onlyone sample application.

    My need is that if I try to launch any application with arguments ,If the application wont start successfully with the arguments I need to launch the application without arguments.

    You can ignore about WindowsFormsApp2 app.This app I just created for testing

    Monday, February 25, 2019 4:27 AM
  • Hi Joy123456,

    Your feedback confused me.

    Do you mean you use three forms in the one project WindowsFormApp3? Run the Form3, and then open the Form1 or Form2 according to the argument? 

    When you use the code below, no argument is passed by default. Hence, there is no form open.

      [STAThread]
            static void Main(string[] args)
            {
                //Application.EnableVisualStyles();
                //Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new Form3());
    
    
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "Launch")
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new Form1());
                    }
                    else if (args[i] == "Exception")
                    {
                        MessageBox.Show("Launch failed");
                    }
                    else
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new Form2());
                    }
                }
            }

    You could set the argument first. But, if you do this, there is no need to run button click event in Form3.

    Right Click Project> Properties> Debug> Start options

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 25, 2019 6:18 AM
    Moderator
  • Thanks for your try.

    My issue is that I need to launch any application .exe (Example : notepad,notepad++ or any applications that I want to start using process.Start).

    If the argument passed is wrong and if the external(ex:notepad) application throws an exception due to the argument passed.

    Then I need to open the external application without arguments. 

    Please ask if you are not clear

    Monday, February 25, 2019 7:07 AM
  • Hi Joy123456,

    Could you provide the code with your requirement for me to test? I am confused. Both of the feedbacks are dofferent.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 25, 2019 7:13 AM
    Moderator
  • The task as you have described it is simply impossible.  The ONLY way to get status from another application is to use the exit code, and there are no universal rules on how exit codes are set.  Some applications return 0 on success and non-zero for failure.  Some return 1 for success and 0 for failure.  Many applications don't return anything at all.

    For specific applications, you can find what exit codes it returns by experimenting, by running the app with your examples and printing the ExitCode, but there is nothing you can do in the general case.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Joy123456 Tuesday, March 26, 2019 7:57 AM
    Monday, February 25, 2019 7:18 AM
  • Thanks for the replay.I do think you are right.

    If any tricky options available to solve this issue It will be appreciated.

    Any way I will wait for 2 more days so that some one can add good idea if possible. Otherwise I will mark this as answer.

    Tuesday, February 26, 2019 12:35 PM