'C# - wpf: start and close instance of calculator.exe or explorer.exe

I am trying to make an application that starts on windows in order to have a controlled environment, but when I try to open explorer or the windows calculator, the process opens and closes instantly even if the calculator actually remains open and consequently does not I can add and remove the application to the navBar.

If I go to check in the active processes I actually do not find the pid of the process that I started

How can I manage these two windows processes?

This is my code:

            var process = Process.Start(program.Path);

            process.EnableRaisingEvents = true;
            // Add icon in bar
            var item = AddIconToApplicationBar(program, process, true);
            var bg = new BackgroundWorker();

            bg.DoWork += (ss, ee) =>
            {
                _logger.Information($"User:{User} Started Program:{program.Name} Auto Start:{autoStart}");
                process?.WaitForExit();
            };

            bg.RunWorkerCompleted += (ss, ee) =>
            {
                _logger.Information($"Exit Program:{program.Name} exitCode: {process?.ExitCode}");

                // Remove icon in bar
                RemoveIconToApplicationBar(item);

                RemoveRunningProgram(process);

                switch (program.Recovery)
                {
                    case Recovery.NoAction:
                        break;
                    case Recovery.UseExitCode:
                        if (process?.ExitCode != 0)
                        {
                            _logger.Information($"Program {program.Name} restarted exitCode: {process?.ExitCode}");
                            StartProcess(program);
                        }

                        break;
                    case Recovery.RestartAlways:
                        _logger.Information($"Program {program.Name} restarted exitCode: {process?.ExitCode}");
                        StartProcess(program);
                        break;
                }
            };

            bg.RunWorkerAsync();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source