• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 776
  • Last Modified:

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

0
stephenlecomptejr
Asked:
stephenlecomptejr
  • 2
1 Solution
 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now