From 28a7898071896bff7ac48fb53b49ccb547ec93fc Mon Sep 17 00:00:00 2001 From: CreepyCrafter24 <33260128+CreepyCrafter24@users.noreply.github.com> Date: Fri, 29 Nov 2019 23:17:46 +0100 Subject: [PATCH] Fixed execution --- cashew/MainForm.Designer.cs | 18 +++++++--- cashew/MainForm.cs | 67 +++++++++++++++++++++++-------------- cashew/MainForm.resx | 15 +++++---- 3 files changed, 64 insertions(+), 36 deletions(-) diff --git a/cashew/MainForm.Designer.cs b/cashew/MainForm.Designer.cs index a61da87..0169a43 100644 --- a/cashew/MainForm.Designer.cs +++ b/cashew/MainForm.Designer.cs @@ -132,6 +132,7 @@ this.nightmodehide = new MetroFramework.Controls.MetroPanel(); this.pythonOpenFileDialog = new System.Windows.Forms.OpenFileDialog(); this.pythonSaveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.buttonFix = new System.Windows.Forms.Timer(this.components); this.languageTabControl.SuspendLayout(); this.cstab.SuspendLayout(); this.csediterrorpanel.SuspendLayout(); @@ -156,7 +157,7 @@ this.languageTabControl.Controls.Add(this.infotab); this.languageTabControl.Location = new System.Drawing.Point(23, 63); this.languageTabControl.Name = "languageTabControl"; - this.languageTabControl.SelectedIndex = 0; + this.languageTabControl.SelectedIndex = 2; this.languageTabControl.Size = new System.Drawing.Size(797, 433); this.languageTabControl.TabIndex = 0; this.languageTabControl.UseSelectable = true; @@ -258,22 +259,23 @@ this.csediterrors.Text = "Ready"; this.csediterrors.Click += new System.EventHandler(this.metroLabel2_Click); // - // cseditcode + // cseditCode // this.cseditCode.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.cseditCode.BackColor = System.Drawing.Color.White; this.cseditCode.ConvertTabsToSpaces = true; + this.cseditCode.Highlighting = null; this.cseditCode.IsIconBarVisible = true; this.cseditCode.LineViewerStyle = ICSharpCode.TextEditor.Document.LineViewerStyle.FullRow; this.cseditCode.Location = new System.Drawing.Point(6, 47); - this.cseditCode.Name = "cseditcode"; + this.cseditCode.Name = "cseditCode"; this.cseditCode.RightToLeft = System.Windows.Forms.RightToLeft.No; this.cseditCode.Size = new System.Drawing.Size(780, 291); this.cseditCode.TabIndex = 9; this.cseditCode.TabStop = false; - this.cseditCode.Text = resources.GetString("cseditcode.Text"); + this.cseditCode.Text = resources.GetString("cseditCode.Text"); // // htmltab // @@ -486,7 +488,7 @@ this.pythonCode.Name = "pythonCode"; this.pythonCode.Size = new System.Drawing.Size(783, 341); this.pythonCode.TabIndex = 11; - this.pythonCode.Text = "print(\"Hello world!\")"; + this.pythonCode.Text = "import time\r\nprint(\"Hello world!\")\r\ntime.sleep(1)"; // // pythonSave // @@ -1124,6 +1126,11 @@ // this.pythonSaveFileDialog.Filter = "Python Files|*.py"; // + // buttonFix + // + this.buttonFix.Enabled = true; + this.buttonFix.Tick += new System.EventHandler(this.buttonFix_Tick); + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -1261,6 +1268,7 @@ private ICSharpCode.TextEditor.TextEditorControl cseditCode; private ICSharpCode.TextEditor.TextEditorControl htmlText; private ICSharpCode.TextEditor.TextEditorControl pythonCode; + private System.Windows.Forms.Timer buttonFix; } } diff --git a/cashew/MainForm.cs b/cashew/MainForm.cs index 698323b..03b7c93 100644 --- a/cashew/MainForm.cs +++ b/cashew/MainForm.cs @@ -21,6 +21,7 @@ using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading; using System.Windows.Forms; +using ThreadState = System.Threading.ThreadState; #pragma warning disable IDE1006 @@ -29,7 +30,7 @@ namespace cashew public partial class MainForm : MetroForm { #region General - + ThreadState[] runningStates = new ThreadState[] { ThreadState.Background, ThreadState.Running, ThreadState.StopRequested, ThreadState.WaitSleepJoin }; private IMetroControl[] metroControls; private Control[] normalControls; private ToolStripMenuItem[] menuItems; @@ -108,11 +109,17 @@ namespace cashew Refresh(); } + private void buttonFix_Tick(object sender, EventArgs e) + { + pythonRun.Text = pythonScript != null && runningStates.Contains(pythonScript.ThreadState) ? "Stop" : "Run"; + cseditrun.Text = csScript != null && runningStates.Contains(csScript.ThreadState) ? "Stop" : "Run"; + } + #endregion General #region CS - Thread script; + Thread csScript; private void metroLabel2_Click(object sender, EventArgs e) => MetroMessageBox.Show(this, csediterrors.Text, "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error); private void metroPanel1_Click(object sender, EventArgs e) => MetroMessageBox.Show(this, csediterrors.Text, "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -177,9 +184,9 @@ namespace cashew private void cseditrun_Click(object sender, EventArgs e) { - if ((script != null) && script.ThreadState == System.Threading.ThreadState.Running) + if ((csScript != null) && runningStates.Contains(csScript.ThreadState)) { - script.Abort(); + csScript.Abort(); } else { @@ -204,21 +211,22 @@ namespace cashew if (results.Errors.HasErrors) throw new InvalidOperationException(string.Join("\r\n\r\n", results.Errors.OfType().Select(s => "Error in line " + s.Line.ToString() + ": " + s.ErrorNumber + " - " + s.ErrorText).ToArray())); cseditrun.Text = "Stop"; - script = new Thread(() => + csScript = new Thread(() => { try { _ = results.CompiledAssembly.EntryPoint.Invoke(null, null); } - finally + catch (Exception e1) { - Invoke((MethodInvoker)delegate () - { - cseditrun.Text = "Run"; - }); + if (!e1.tryCast(out ThreadAbortException ex)) + Invoke((MethodInvoker)delegate () + { + MetroMessageBox.Show(this, e1.Message, "Execution Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); + }); } }); - script.Start(); + csScript.Start(); csediterrors.Text = "Ready"; } catch (Exception e1) @@ -226,7 +234,7 @@ namespace cashew csediterrors.Text = e1.Message; } } - cseditrun.Text = script.ThreadState == System.Threading.ThreadState.Running ? "Stop" : "Run"; + cseditrun.Text = csScript.ThreadState == System.Threading.ThreadState.Running ? "Stop" : "Run"; } private void cseditopen_Click(object sender, EventArgs e) @@ -491,7 +499,7 @@ namespace cashew #endregion HTML #region Python - + Thread pythonScript; private void pythonOpen_Click(object sender, EventArgs e) { if (pythonOpenFileDialog.ShowDialog() == DialogResult.OK) @@ -524,21 +532,30 @@ namespace cashew private void pythonRun_Click(object sender, EventArgs e) { - new Thread(() => + if ((pythonScript != null) && runningStates.Contains(pythonScript.ThreadState)) { - ScriptEngine engine = Python.CreateEngine(); - try + pythonScript.Abort(); + } + else + { + pythonScript = new Thread(() => { - engine.Execute(pythonCode.Text); - } - catch (Exception e1) - { - Invoke((MethodInvoker)delegate () + ScriptEngine engine = Python.CreateEngine(); + try { - MetroMessageBox.Show(this, e1.Message, "Execution Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); - }); - } - }).Start(); + engine.Execute(pythonCode.Text); + } + catch (Exception e1) + { + if (!e1.tryCast(out ThreadAbortException ex)) + Invoke((MethodInvoker)delegate () + { + MetroMessageBox.Show(this, e1.Message, "Execution Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); + }); + } + }); + pythonScript.Start(); + } } #endregion Python diff --git a/cashew/MainForm.resx b/cashew/MainForm.resx index 03960bb..a32d190 100644 --- a/cashew/MainForm.resx +++ b/cashew/MainForm.resx @@ -117,7 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + using System; using System.Windows.Forms; @@ -147,19 +147,22 @@ IF YOU WANT A PROFESSIONAL PROGRAM, DON'T USE THIS! 153, 17 - 423, 17 + 299, 17 - 574, 17 + 450, 17 - 730, 17 + 606, 17 - 890, 17 + 766, 17 - 1064, 17 + 940, 17 + + + 1109, 17