question

MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 asked TimonYang-MSFT edited

Terminate the proses.Start

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);
             }
         }
    
    
     }
 }



dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered MiPakTeh-6272 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Viorel-1 ,

I try to find where the code use "p?.kill and p = null" method but not find. If there have but not succed.
Can you show the real coding.
Thank you .

0 Votes 0 ·
MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 answered MiPakTeh-6272 commented

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_);
         }
    
    
     }
 }


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang..
Begin_ is Start_1 Class.

0 Votes 0 ·

I found the reason for the error, we should not use using statement in the OpenApplication method, it will dispose the process.

         public static void OpenApplication()
         {
    
             try
             {
                 myProcess = new Process();
                 myProcess.StartInfo.UseShellExecute = true;
                 myProcess.StartInfo.FileName = "cmd";
                 myProcess.StartInfo.CreateNoWindow = true;
                 myProcess.Start();
             }
             catch (Exception e)
             {
                 Console.WriteLine(e.Message);
             }
         }

0 Votes 0 ·

Thank you very much TimonYang,
Code working good.

0 Votes 0 ·