-1

When a WinFromApp.exe written in C# is closing, it finally should start another Prog.exe.

Important, the WinFromApp.exe shouldn't wait for a result of Prog.exe. The WinFromApp.exe should completly close while Prog.exe runs.

[This is my 2nd using of this forum for place a Question.]

I still have programmed many with Dreamweaver MS-Office-VBA...

Visual Studio (2017) programming is new for me. Because of this, I need also to know where to place the code/code-part(s).

I have as From "Welcome.cs" ["++...++" in Code marks the done including from Anu Viswan.]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using Microsoft.VisualBasic;
using Microsoft.Win32;
using HardwareHelperLib;

namespace Welcome
{
    public partial class Welcome : Form
    {

        public Welcome()
        {
            InitializeComponent();
            +Application.ApplicationExit += Application_ApplicationExit;+
        }

        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            ...;
        }

        private void wb_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            ...;
        }

        private void Welcome_Load(object sender, EventArgs e)
        {

            ...;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ...;
        }

        ++private void Application_ApplicationExit(object sender, EventArgs e)
        {
            var process = new ProcessStartInfo("notepad.exe");
            Process.Start(process);
        }
        private void Welcome_FormClosing(object sender, FormClosingEventArgs e)
        {
            var process = new ProcessStartInfo("notepad.exe");
            Process.Start(process);
        }++
    }
}

And Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Welcome
{
    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Welcome());
        }
    }
}

I have to place this:

Application.ApplicationExit += Application_ApplicationExit;

by here [+...+], and it is working = THANK YOU VERY MUCH !!!

This question already has an answer here: How to run code before program exit? [duplicate]

I have looked at this link, but not found the answer of my Question...

Because as I has written in my Question:

Visual Studio (2017) programming is new for me. Because of this, I need also to know where to place the code/code-part(s).

Sorry, until yesterday the last years I used stackoverflow only as a Reader.

So I am also new at stackoverflow doing Questions.

HWKWvL
  • 15
  • 6

1 Answers1

1

Assuming your application is WinForm app, you can do this with ApplicationExit event.

First you need to subscribe for application exit and FormClosing event.

Application.ApplicationExit += Application_ApplicationExit;

and then, in the event, you can use Process.Start to invoke the second exe.

private void Application_ApplicationExit(object sender, EventArgs e)
{
    var process = new ProcessStartInfo("notepad.exe");
    Process.Start(process);
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    var process = new ProcessStartInfo("notepad.exe");
    Process.Start(process);
}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51