I am opening windows calculator in my MDI windows application and it gets open
through a child form which is a part of mdi form.I am not able to close the windows calc
even when my form gets closed.
code:

System.Diagnostics.Process p = null;
 public bool calcinstance()
        {

            if (p == null)
            {
                p = new Process();
                p.StartInfo.FileName = "Calc.exe";
                p.Start();
                return false;
            }
            else
            {
                p.Close();
                p.Dispose();
                return true;
            }
           
        }

on button click I want to close calc.

p.close();
or
p.kill(); does not work .No error Message.

Edited 4 Years Ago by __avd: Added [code] tags.

I am calling my function on button click event.
I did try CloseMainWindow but then if i click calc menustrip again it does not open calc.exe but
shows the error saying "no process is attached with this object " and stops at closemainwindow() line.

Sounds like CloseMainWindow() is not setting the process to null. The process class has a few events you can subscribe to once you enable the events. Let me know if this makes sense to you:

//flag used to request a calculator while the current one is disposing
        bool RequestNewCalc = false;
        //the calculator process
        Process pCalc;
        
        //creates a single instance of calculator window, and will destroy the current
        //one if it is open, then will flag for a new one to open once the current one is
        //exited and disposed of
        public void CreateCalc()
        {
            //if the calculator process is null, create the process
            if (pCalc == null)
            {
                pCalc = new Process();
                pCalc.StartInfo.FileName = "calc.exe";
                pCalc.Start();
                //these are the events I described
                pCalc.EnableRaisingEvents = true;
                pCalc.Exited += new EventHandler(pCalc_Exited);
            }
            else
            {
                //If the calculator is already open, close it and request a new one
                //that will be created after the first one is disposed 
                CloseCalc();
                RequestNewCalc = true;
            }
        }
        //closes the calculator window by closing its main window
        public void CloseCalc()
        {
            if (pCalc != null)
                pCalc.CloseMainWindow();
        }
        //called when the calculator is exited (by the window or programatically)
        void pCalc_Exited(object sender, EventArgs e)
        {
            //Dispose of the calculator and set to null
            pCalc.Dispose();
            pCalc = null;
            //Create a new calc window if the flag is set, and clear the flag
            if (RequestNewCalc)
            {
                RequestNewCalc = false;
                CreateCalc();
            }
        }
        //Call the above methods from event handlers in your code
        private void btnOpen_Click(object sender, EventArgs e)
        {
            CreateCalc();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            CloseCalc();
        }

Edited 4 Years Ago by skatamatic: n/a

This article has been dead for over six months. Start a new discussion instead.