Home>

I am creating a calculator that can be clicked in C#
In "var IniEvent = new Initialize()" of Main window process
It will be an infinite loop and the process will not proceed.

Applicable source code
namespace Calculator
{
    public partial class MainWindow: Window
    {
        ///<summary>      /// constructor
        ///</summary>      public MainWindow()
        {
            InitializeComponent();
            // Event initialization
            var IniEvent = new Initialize();
            IniEvent.InitializeEvent();
        }
    }
}
namespace Calculator
{
    public partial class Initialize: MainWindow
    {
        ///<summary>      /// Event initialization
        ///</summary>      public void InitializeEvent()
        {
            //rootGrid looks for the following controls
            foreach (var ctrl in LogicalTreeHelper.GetChildren(rootGrid))
            {
                // ignore all but buttons
                if (!(ctrl is Button))
                {
                    continue;
                }
                // process registration when the button is clicked
                (ctrl as Button).Click += (sender, e) =>              {
                    string inKey = (sender as Button).Content.ToString();
                    switch (inKey)
                    {
                        //clear
                        case "AC":
                            formula.Text = "0";
                            break;
                        case "=":
                            if (formula.Text == MessageList.CFNM0001)
                            {
                                formula.Text = "0";
                            }
                            else
                            {
                                //Display the calculation result
                                var Calc = new CalculateProcess();
                                formula.Text = Calc.Calculator(formula.Text);
                            }
                            break;
                        default:
                            if (formula.Text.Length >= ItemProperty.FormulaMax)
                            {
                                break;
                            }
                            else if (formula.Text == "0"&&(inKey != "/"&&inKey != "*"&&inKey != "-"&&inKey != "+"))
                            {
                                formula.Text = inKey;
                            }
                            else if (formula.Text == MessageList.CFNM0001)
                            {
                                //If 0 is displayed and a number is entered, set that value
                                formula.Text = inKey;}
                            else
                            {
                                // Add the entered value to the expression
                                formula.Text += inKey;
                            }
                            break;
                    }
                };
            }
        }
    }
}
Addendum
namespace Calculator
{
    public partial class CalculateProcess: MainWindow
    {
        ///<summary>      /// calculation execution
        ///</summary>      ///<param name="formula">Calculation formula</param>      ///<returns>calculation result</returns>      public string Calculator(string formula)
        {
            try
            {
                var p = new Process();
                // Get system folder
                string systemFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);
                // assemble the full PowerShell path
                string powerShellFullPath = System.IO.Path.Combine(systemFolder, @"WindowsPowerShell\v1.0\powershell.exe");
                // executable file
                p.StartInfo.FileName = powerShellFullPath;
                // parameter (formula)
                p.StartInfo.Arguments = "-Command" + formula;
                // do not redirect standard input
                p.StartInfo.RedirectStandardInput = false;
                // redirect standard input
                p.StartInfo.RedirectStandardOutput = true;

                // do not use shell function
                p.StartInfo.UseShellExecute = false;
                // do not open window
                p.StartInfo.CreateNoWindow = true;
                // run
                p.Start();
                // read a line of results
                string calcResult = p.StandardOutput.ReadLine();
                p.WaitForExit();
                p.Close();
                if (string.IsNullOrEmpty(calcResult))
                {
                    // No calculation result (calculation failure)
                    return MessageList.CFNM0001;
                }
                else
                {
                    // return the calculation result
                    return calcResult;
                }
            }
            catch
            {
                return MessageList.CFNM0001;
            }
        }
    }
}
What I tried

I tried to check it with "Instantiation infinite loop", "Main infinite loop", etc., but it didn't come out, and the cause is unknown.

Why is it in an infinite loop?
Also, how should we improve?
If i do not mind, please teach me

Supplementary information (FW/tool ​​version, etc.)

Windows10 Pro 64bit
Visual Studio 2015

Please let me know if you have any other necessary information.

Solution method (done)
namespace Calculator
{
    public partial class Initialize
    {
        ///<summary>      /// Event initialization
        ///</summary>      public void InitializeEvent(MainWindow w)
        {
            //rootGrid looks for the following controls
            foreach (var ctrl in LogicalTreeHelper.GetChildren(w.rootGrid))
            {
                // ignore all but buttons
                if (!(ctrl is Button))
                {
                    continue;
                }
                // process registration when the button is clicked
                (ctrl as Button).Click += (sender, e) =>              {
                    string inKey = (sender as Button).Content.ToString();
                    switch (inKey)
                    {
                        //clear
                        case "AC":
                            w.formula.Text = "0";
                            break;
                        case "=":
                            if (w.formula.Text == MessageList.CFNM0001)
                            {
                                w.formula.Text = "0";
                            }
                            else
                            {
                                //Display the calculation result
                                var Calc = new CalculateProcess();
                                w.formula.Text = Calc.Calculator(w.formula.Text);
                            }
                            break;
                        default:
                            if (w.formula.Text.Length >= ItemProperty.FormulaMax)
                            {
                                break;
                            }
                            else if (w.formula.Text == "0"&&(inKey != "/"&&inKey != "*"&&inKey != "-"&&inKey != "+"))
                            {
                                w.formula.Text = inKey;
                            }
                            else if (w.formula.Text == MessageList.CFNM0001)
                            {
                                //If 0 is displayed and a number is entered, set that value
                                w.formula.Text = inKey;
                            }
                            else
                            {
                                // Add the entered value to the expression
                                w.formula.Text += inKey;
                            }
                            break;
                    }
                };
            }
        }
    }
}
c#