locked
I want to invoke exe from my C# code, after invoking the exe my c# application should be closed imediatly. RRS feed

  • Question

  • Hi,

    I want to invoke the exe from my c# code . Using below code I can achieve this.

                  myProcess.StartInfo.FileName = @"C:\MyApp\Setup.exe";
                    myProcess.StartInfo.UseShellExecute = true;
                    myProcess.StartInfo.Verb = "runas";
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.Start();

    But I need to close my application (that is c# app) immediately after the exe file invocation and setup wizard should continues. 

    Thanks

    K.Arivazhagan




    Friday, November 23, 2018 7:22 AM

All replies

  • myProcess.StartInfo.FileName = @"C:\MyApp\Setup.exe";
    myProcess.StartInfo.UseShellExecute = true;
    myProcess.StartInfo.Verb = "runas";
    myProcess.StartInfo.CreateNoWindow = true;
    myProcess.Start();

    Application.Current.Shutdown();

    Friday, November 23, 2018 1:22 PM
  • How you close your program depends on what kind of program it is. Is it a console, WinForms or WPF program?

    Console:

    • Exit(0);

    Winform and WPF: In general it is best to tell the main application window to close. Otherwise, you run the risk of leaving things incomplete or worse. If there is only a single main window and you have access to it then:

    WinForms:

    • Form.Close() where 'Form' is the instantiated Form object itself.
    • Application.MainWindow.Close()

    Wpf:

    • Application.Current.MainWindow.Close();

    Alternatively, you can force the application object itself to self immolate. This works in both WPF and WinForms.

    WinForms:

    • Application.Exit();
    • Environment.Exit();

    Wpf:

    • Application.Current.ShutDown();
    • Environment.Exit();

    There are some gotcha's. If there is a worker thread running then you could find yourself with no UI but the worker is still running and the application's resources are not cleaned up. The BackgroundWorker class is good about terminating when the application terminates but the older Thread class is not.


    Richard Lewis Haggard

    Friday, November 23, 2018 10:09 PM