How to force Application.Exit() to work for basic C# program?

I have the following code that utilizes a form but no matter what happens the program does not end and the form remains on the screen until someone clicks on the X button.  Please forgive since I'm new to C# and not sure why the following code does not work - if you could provide the proper syntax or arrangement of code to make this work - would be much appreciated!

The code's intent is to open up Revit and worksharing.  I do have Revit installed but not Worksharing .exe.  Thus I always get at least one message box telling me I don't have the program installed.  Perhaps having the message box come up causes a delay in the Application.Exit()  But even when I click on OK... I still have the form come up and it goes to Application.Exit() and doesn't fire.

Perhaps I could use something other than a form?  But I didn't want to use a console program nor a .bat file - I wanted to take an opportunity to learn C# sharp.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace FKPBIMOpener
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            if (File.Exists(@"C:\Program Files\Autodesk Revit Architecture 2010\Program\Revit.exe") == false)
                //shows message if testFile exist 
                MessageBox.Show(@"C:\Program Files\Autodesk Revit Architecture 2010\Program\Revit.exe - doesn't exist on your machine to run!");

            else
                try
                {
               
                Process.Start(@"C:\Program Files\Autodesk Revit Architecture 2010\Program\Revit.exe");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

            if (File.Exists(@"C:\Program Files (x86)\Autodesk\Worksharing Monitor for Autodesk Revit 2010\WorksharingMonitor.exe") == false)            
                //shows message if testFile exist 
                MessageBox.Show(@"C:\Program Files (x86)\Autodesk\Worksharing Monitor for Autodesk Revit 2010\WorksharingMonitor.exe - doesn't exist on your machine to run!");
            else
                try
                {
                    Process.Start(@"C:\Program Files (x86)\Autodesk\Worksharing Monitor for Autodesk Revit 2010\WorksharingMonitor.exe");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }


            Application.Exit();
            

        }
    }
}

Open in new window

LVL 1
stephenlecomptejrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
try
            {
                if (File.Exists(@"C:\Program Files\Autodesk Revit Architecture 2010\Program\Revit.exe") == false)
                {
                    MessageBox.Show(@"C:\Program Files\Autodesk Revit Architecture 2010\Program\Revit.exe - doesn't exist on your machine to run!");
                }
                else
                {
                    Process.Start(@"C:\Program Files\Autodesk Revit Architecture 2010\Program\Revit.exe");
                }
                if (File.Exists(@"C:\Program Files (x86)\Autodesk\Worksharing Monitor for Autodesk Revit 2010\WorksharingMonitor.exe") == false)
                {
                    //shows message if testFile exist
                    MessageBox.Show(@"C:\Program Files (x86)\Autodesk\Worksharing Monitor for Autodesk Revit 2010\WorksharingMonitor.exe - doesn't exist on your machine to run!");

                }
                else
                {
                    Process.Start(@"C:\Program Files (x86)\Autodesk\Worksharing Monitor for Autodesk Revit 2010\WorksharingMonitor.exe");
                }

            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.ToString());
            }
            finally
            {
                Application.Exit();
            }
0
Tech_StigCommented:
Application.Exit() is being called in the constructor of the form. It's basically trying to exit the application before it really exists. Try generating the form_load event and pasting your code in there or at least paste your exit code there.

To generate the load event bring up the form in design mode, right-click the form and chose properties. In the properties pane click the events button (looks like a lightning bolt) scroll till you see Load and doubleclick in the text area next to it.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stephenlecomptejrAuthor Commented:
Tech_Stig,

Thank for the explanation and where to find Form_Load - I would have had to ask another qeustion.
Yours was the only solution that worked!  Please look for my name in the future!
0
Tech_StigCommented:
Glad I could help! :) Additionally you could also create a c# console application to do this. But now you have a form to drag controls out onto and play! :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.