none
close the parent process when hotkey is pressed. RRS feed

  • Question

  • hello. first, sorry for such a newbie question 

    i've made a simple app that will download an exe from a server then execute it as child, the child will close the parent by it's file name if "Esc" key is pressed, is there a way in C# to make that child close the parent regardless it's name ?

    thanks in advanced.

    Monday, July 30, 2018 2:37 PM

All replies

  • Well, handover the pid when starting your child process and send a message to it, when you want to close it. On the other hand, why not starting the process and exiting your program?
    Monday, July 30, 2018 8:11 PM
  • i don't have the child exe yet, some friend told me to use this code 

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
            if (keyData == Keys.Escape) {
                this.Close(); // shutdown the Application instead of closing
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

    the problem is that when i compile it i see a form, i want this process to run in the background, also, when i click on the form and i press Esc, the form closes but the parent (the app that executed this app) is still running

    or as an alt to this, i want to close a process by pressing Esc Key, where i can type the process name in the code 

    Monday, July 30, 2018 10:47 PM
  • Hi Tarwada,

    Thank you for posting here.

    For your question, please try the code below.

    using System;
    using System.Diagnostics;
    using System.Windows.Forms;
    namespace ParentProcess
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                
            }
            private void btn_openChildProcess(object sender, EventArgs e)
            {
                Process.Start(@"D:\New folder\CSharpSln\ChildProcess\bin\Debug\ChildProcess.exe");
            }
        }
    }
    
    using System;
    using System.Diagnostics;
    using System.Windows.Forms;
    namespace ChildProcess
    {
        public partial class CPP : Form
        {
            public CPP()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
            }
            private void CloseParentProcess()
            {
                Process.GetProcessById(6676).Kill();
            }
            //Press ESC to close ParentProcess
            private void FrmHospital_KeyDown(object sender, KeyEventArgs e)
            {
                Keys k = e.KeyCode;
                if (k == Keys.Escape)
                {
                    CloseParentProcess();
                }
            }
        }
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 31, 2018 8:17 AM
    Moderator
  •  YMMD..

    Process.GetProcessById(6676).Kill();

    There is no guarantee, that the parent process has always the same, static PID. 

    Tuesday, July 31, 2018 9:01 AM