Fixed execution

This commit is contained in:
CreepyCrafter24 2019-11-29 23:17:46 +01:00
parent 6a19967c46
commit 28a7898071
3 changed files with 64 additions and 36 deletions

View File

@ -132,6 +132,7 @@
this.nightmodehide = new MetroFramework.Controls.MetroPanel(); this.nightmodehide = new MetroFramework.Controls.MetroPanel();
this.pythonOpenFileDialog = new System.Windows.Forms.OpenFileDialog(); this.pythonOpenFileDialog = new System.Windows.Forms.OpenFileDialog();
this.pythonSaveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.pythonSaveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.buttonFix = new System.Windows.Forms.Timer(this.components);
this.languageTabControl.SuspendLayout(); this.languageTabControl.SuspendLayout();
this.cstab.SuspendLayout(); this.cstab.SuspendLayout();
this.csediterrorpanel.SuspendLayout(); this.csediterrorpanel.SuspendLayout();
@ -156,7 +157,7 @@
this.languageTabControl.Controls.Add(this.infotab); this.languageTabControl.Controls.Add(this.infotab);
this.languageTabControl.Location = new System.Drawing.Point(23, 63); this.languageTabControl.Location = new System.Drawing.Point(23, 63);
this.languageTabControl.Name = "languageTabControl"; this.languageTabControl.Name = "languageTabControl";
this.languageTabControl.SelectedIndex = 0; this.languageTabControl.SelectedIndex = 2;
this.languageTabControl.Size = new System.Drawing.Size(797, 433); this.languageTabControl.Size = new System.Drawing.Size(797, 433);
this.languageTabControl.TabIndex = 0; this.languageTabControl.TabIndex = 0;
this.languageTabControl.UseSelectable = true; this.languageTabControl.UseSelectable = true;
@ -258,22 +259,23 @@
this.csediterrors.Text = "Ready"; this.csediterrors.Text = "Ready";
this.csediterrors.Click += new System.EventHandler(this.metroLabel2_Click); 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) 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.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.cseditCode.BackColor = System.Drawing.Color.White; this.cseditCode.BackColor = System.Drawing.Color.White;
this.cseditCode.ConvertTabsToSpaces = true; this.cseditCode.ConvertTabsToSpaces = true;
this.cseditCode.Highlighting = null;
this.cseditCode.IsIconBarVisible = true; this.cseditCode.IsIconBarVisible = true;
this.cseditCode.LineViewerStyle = ICSharpCode.TextEditor.Document.LineViewerStyle.FullRow; this.cseditCode.LineViewerStyle = ICSharpCode.TextEditor.Document.LineViewerStyle.FullRow;
this.cseditCode.Location = new System.Drawing.Point(6, 47); 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.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.cseditCode.Size = new System.Drawing.Size(780, 291); this.cseditCode.Size = new System.Drawing.Size(780, 291);
this.cseditCode.TabIndex = 9; this.cseditCode.TabIndex = 9;
this.cseditCode.TabStop = false; this.cseditCode.TabStop = false;
this.cseditCode.Text = resources.GetString("cseditcode.Text"); this.cseditCode.Text = resources.GetString("cseditCode.Text");
// //
// htmltab // htmltab
// //
@ -486,7 +488,7 @@
this.pythonCode.Name = "pythonCode"; this.pythonCode.Name = "pythonCode";
this.pythonCode.Size = new System.Drawing.Size(783, 341); this.pythonCode.Size = new System.Drawing.Size(783, 341);
this.pythonCode.TabIndex = 11; this.pythonCode.TabIndex = 11;
this.pythonCode.Text = "print(\"Hello world!\")"; this.pythonCode.Text = "import time\r\nprint(\"Hello world!\")\r\ntime.sleep(1)";
// //
// pythonSave // pythonSave
// //
@ -1124,6 +1126,11 @@
// //
this.pythonSaveFileDialog.Filter = "Python Files|*.py"; this.pythonSaveFileDialog.Filter = "Python Files|*.py";
// //
// buttonFix
//
this.buttonFix.Enabled = true;
this.buttonFix.Tick += new System.EventHandler(this.buttonFix_Tick);
//
// MainForm // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -1261,6 +1268,7 @@
private ICSharpCode.TextEditor.TextEditorControl cseditCode; private ICSharpCode.TextEditor.TextEditorControl cseditCode;
private ICSharpCode.TextEditor.TextEditorControl htmlText; private ICSharpCode.TextEditor.TextEditorControl htmlText;
private ICSharpCode.TextEditor.TextEditorControl pythonCode; private ICSharpCode.TextEditor.TextEditorControl pythonCode;
private System.Windows.Forms.Timer buttonFix;
} }
} }

View File

@ -21,6 +21,7 @@ using System.Runtime.Serialization.Formatters.Binary;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using ThreadState = System.Threading.ThreadState;
#pragma warning disable IDE1006 #pragma warning disable IDE1006
@ -29,7 +30,7 @@ namespace cashew
public partial class MainForm : MetroForm public partial class MainForm : MetroForm
{ {
#region General #region General
ThreadState[] runningStates = new ThreadState[] { ThreadState.Background, ThreadState.Running, ThreadState.StopRequested, ThreadState.WaitSleepJoin };
private IMetroControl[] metroControls; private IMetroControl[] metroControls;
private Control[] normalControls; private Control[] normalControls;
private ToolStripMenuItem[] menuItems; private ToolStripMenuItem[] menuItems;
@ -108,11 +109,17 @@ namespace cashew
Refresh(); 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 #endregion General
#region CS #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 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); 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) 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 else
{ {
@ -204,21 +211,22 @@ namespace cashew
if (results.Errors.HasErrors) if (results.Errors.HasErrors)
throw new InvalidOperationException(string.Join("\r\n\r\n", results.Errors.OfType<CompilerError>().Select(s => "Error in line " + s.Line.ToString() + ": " + s.ErrorNumber + " - " + s.ErrorText).ToArray())); throw new InvalidOperationException(string.Join("\r\n\r\n", results.Errors.OfType<CompilerError>().Select(s => "Error in line " + s.Line.ToString() + ": " + s.ErrorNumber + " - " + s.ErrorText).ToArray()));
cseditrun.Text = "Stop"; cseditrun.Text = "Stop";
script = new Thread(() => csScript = new Thread(() =>
{ {
try try
{ {
_ = results.CompiledAssembly.EntryPoint.Invoke(null, null); _ = results.CompiledAssembly.EntryPoint.Invoke(null, null);
} }
finally catch (Exception e1)
{ {
Invoke((MethodInvoker)delegate () if (!e1.tryCast(out ThreadAbortException ex))
{ Invoke((MethodInvoker)delegate ()
cseditrun.Text = "Run"; {
}); MetroMessageBox.Show(this, e1.Message, "Execution Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
});
} }
}); });
script.Start(); csScript.Start();
csediterrors.Text = "Ready"; csediterrors.Text = "Ready";
} }
catch (Exception e1) catch (Exception e1)
@ -226,7 +234,7 @@ namespace cashew
csediterrors.Text = e1.Message; 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) private void cseditopen_Click(object sender, EventArgs e)
@ -491,7 +499,7 @@ namespace cashew
#endregion HTML #endregion HTML
#region Python #region Python
Thread pythonScript;
private void pythonOpen_Click(object sender, EventArgs e) private void pythonOpen_Click(object sender, EventArgs e)
{ {
if (pythonOpenFileDialog.ShowDialog() == DialogResult.OK) if (pythonOpenFileDialog.ShowDialog() == DialogResult.OK)
@ -524,21 +532,30 @@ namespace cashew
private void pythonRun_Click(object sender, EventArgs e) private void pythonRun_Click(object sender, EventArgs e)
{ {
new Thread(() => if ((pythonScript != null) && runningStates.Contains(pythonScript.ThreadState))
{ {
ScriptEngine engine = Python.CreateEngine(); pythonScript.Abort();
try }
else
{
pythonScript = new Thread(() =>
{ {
engine.Execute(pythonCode.Text); ScriptEngine engine = Python.CreateEngine();
} try
catch (Exception e1)
{
Invoke((MethodInvoker)delegate ()
{ {
MetroMessageBox.Show(this, e1.Message, "Execution Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); engine.Execute(pythonCode.Text);
}); }
} catch (Exception e1)
}).Start(); {
if (!e1.tryCast(out ThreadAbortException ex))
Invoke((MethodInvoker)delegate ()
{
MetroMessageBox.Show(this, e1.Message, "Execution Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
});
}
});
pythonScript.Start();
}
} }
#endregion Python #endregion Python

View File

@ -117,7 +117,7 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<data name="cseditcode.Text" xml:space="preserve"> <data name="cseditCode.Text" xml:space="preserve">
<value>using System; <value>using System;
using System.Windows.Forms; using System.Windows.Forms;
@ -147,19 +147,22 @@ IF YOU WANT A PROFESSIONAL PROGRAM, DON'T USE THIS!</value>
<value>153, 17</value> <value>153, 17</value>
</metadata> </metadata>
<metadata name="htmlOptionsMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="htmlOptionsMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>423, 17</value> <value>299, 17</value>
</metadata> </metadata>
<metadata name="htmlSaveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="htmlSaveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>574, 17</value> <value>450, 17</value>
</metadata> </metadata>
<metadata name="htmlOpenFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="htmlOpenFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>730, 17</value> <value>606, 17</value>
</metadata> </metadata>
<metadata name="pythonOpenFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="pythonOpenFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>890, 17</value> <value>766, 17</value>
</metadata> </metadata>
<metadata name="pythonSaveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="pythonSaveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>1064, 17</value> <value>940, 17</value>
</metadata>
<metadata name="buttonFix.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>1109, 17</value>
</metadata> </metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">