Terminate the proses.Start

MiPakTeh 1,476 Reputation points
2021-06-27T03:35:54.68+00:00

I have test a small program.I have 3 executable Training_, Training_1 and Training_2.
Training_ is a code to put Training_1 at startUp windows and Training_1 have a Proses.Start to start Training_2 automatically.
I don't how to stop or close Training_2 using Proses.kill() in Training_1.

Thank.

form code;

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.Threading;

namespace Training_1
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string myFavoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
            Start_1.OpenApplication(myFavoritesPath);


        }

        private void button1_Click(object sender, EventArgs e)
        {
            string myFavoritesPath_ = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
            Start_1.StopApplication(myFavoritesPath_);

        }
    }

}

Class code

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;


namespace Training_1
{

    class Start_1
    {
        public static void OpenApplication(string myFavoritesPath)
        {
            try
            {
                using (Process myProcess = new Process())
                {
                    myProcess.StartInfo.UseShellExecute = false;
                    myProcess.StartInfo.FileName = "C:\\Users\\family\\Desktop\\Training_2.exe";
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.Start();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public static void StopApplication(string myFavoritesPath_)
        {
            try
            {
                using (Process myProcess = Process.Start("C:\\Users\\family\\Desktop\\Training_2.exe"))
                {
                    myProcess.Kill();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }


    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,571 Reputation points
    2021-06-30T07:04:45.753+00:00

    Please note that you opened two instances of Training_2.exe in the original code. They have their own PIDs. When you use myProcess.Kill();, it will only close the one opened in the StopApplication method.

    Try this:

            class Start_1  
            {  
                private static Process myProcess;  
                public static void OpenApplication(string myFavoritesPath)  
                {  
                    try  
                    {  
                        myProcess = new Process()  
                        ...  
                    }  
                    catch (Exception e)  
                    {  
                        Console.WriteLine(e.Message);  
                    }  
                }  
      
                public static void StopApplication(string myFavoritesPath_)  
                {  
                    try  
                    {  
                        myProcess?.Kill();  
                        myProcess = null;  
                    }  
                    catch (Exception e)  
                    {  
                        Console.WriteLine(e.Message);  
                    }  
                }  
    

    This is just how the idea of Viorel-1 is used in your code.

    I wanted to post it as a comment, but the characters exceeded the comment limit, so I had to post it as an answer.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2021-06-27T10:20:26.233+00:00

    Try something like this:

    Process p = null;
    
    private void Form1_Load(object sender, EventArgs e)
    {
       string myFavoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
       p = Start_1.OpenApplication(myFavoritesPath);
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
       p?.Kill();
       p = null;
    }
    
    . . .
    
    public static Process OpenApplication(string myFavoritesPath)
    {
       . . .
       return myProcess;
    }
    
    1 person found this answer helpful.

  2. MiPakTeh 1,476 Reputation points
    2021-06-30T11:11:35.823+00:00

    Hi TimonYang, thank for your feedback.
    I follow your code in Class then in Form code at click button1 we put this code but nothing happen.
    I don't where is wrong .

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace  Training_1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                string myFavoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
                Begin_.OpenApplication(myFavoritesPath);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string myFavoritesPath_ = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
                Begin_.StopApplication(myFavoritesPath_);
            }
    
    
        }
    }