diff --git a/LaserGRBL/MainForm.cs b/LaserGRBL/MainForm.cs index 00a4a0b4..b30a18e8 100644 --- a/LaserGRBL/MainForm.cs +++ b/LaserGRBL/MainForm.cs @@ -35,6 +35,8 @@ public partial class MainForm : Form public MainForm() { + ColorScheme.CurrentScheme = Settings.GetObject("Color Schema", ColorScheme.Scheme.CADDark); + InitializeComponent(); ExceptionManager.ParentMain = this; @@ -105,8 +107,6 @@ public MainForm() GitHub.NewVersion += GitHub_NewVersion; - ColorScheme.CurrentScheme = Settings.GetObject("Color Schema", ColorScheme.Scheme.CADDark); - IconsMgr.PrepareMenuItem(MnConnect, "mdi-power-plug"); IconsMgr.PrepareMenuItem(MnDisconnect, "mdi-power-plug-off"); IconsMgr.PrepareMenuItem(MnWiFiDiscovery, "mdi-wifi"); diff --git a/LaserGRBL/Tools/ThreadClass.cs b/LaserGRBL/Tools/ThreadClass.cs index 7eea5e6a..1927ee99 100644 --- a/LaserGRBL/Tools/ThreadClass.cs +++ b/LaserGRBL/Tools/ThreadClass.cs @@ -16,7 +16,7 @@ public class ThreadObject : ThreadClass private ThreadStart _delegateSub; private ThreadStart _firstRunSub; - public ThreadObject(ThreadStart DelegateSub, int SleepTime, bool AutoDispose, string Name, ThreadStart FirstRunSub, ThreadPriority priority = ThreadPriority.Normal, ApartmentState apartment = ApartmentState.Unknown) : base(SleepTime, AutoDispose, Name, priority) + public ThreadObject(ThreadStart DelegateSub, int SleepTime, bool AutoDispose, string Name, ThreadStart FirstRunSub, ThreadPriority priority = ThreadPriority.Normal, ApartmentState apartment = ApartmentState.Unknown, EventWaitHandle ev = null) : base(SleepTime, AutoDispose, Name, priority, apartment, ev) { _delegateSub = DelegateSub; _firstRunSub = FirstRunSub; @@ -43,18 +43,22 @@ public abstract class ThreadClass : IDisposable { protected ManualResetEvent MustExit; - //checked 26/05/2008 + private EventWaitHandle UserHandle; + + //checked 26/05/2008 protected internal Thread TH; private ApartmentState mApartment; protected internal ThreadPriority mPriority; - protected ThreadClass(int SleepTime, bool AutoDispose, string Name, ThreadPriority priority, ApartmentState apartment = ApartmentState.Unknown) + private WaitHandle[] mHandleArray; + protected ThreadClass(int SleepTime, bool AutoDispose, string Name, ThreadPriority priority, ApartmentState apartment = ApartmentState.Unknown, EventWaitHandle ev = null) { mApartment = apartment; mPriority = priority; + UserHandle = ev; + this.SleepTime = SleepTime; - if (AutoDispose) - System.Windows.Forms.Application.ApplicationExit += this.AutoDispose; + if (AutoDispose) System.Windows.Forms.Application.ApplicationExit += this.AutoDispose; _Name = Name; } @@ -62,7 +66,8 @@ protected ThreadClass(int SleepTime, bool AutoDispose, string Name, ThreadPriori protected virtual bool MustRun() { //return true if must run - return (MustExit != null) && !MustExit.WaitOne(SleepTime, false); + + return mHandleArray != null && (WaitHandle.WaitAny(mHandleArray, SleepTime) != 0); // 0 = MustExit } @@ -91,6 +96,9 @@ public virtual void Start() { if (TH == null) { MustExit = new ManualResetEvent(false); + if (UserHandle != null) mHandleArray = new WaitHandle[] { MustExit, UserHandle }; + else mHandleArray = new WaitHandle[] { MustExit }; + TH = new Thread(Loop); if (mApartment != ApartmentState.Unknown) TH.SetApartmentState(mApartment); TH.Priority = mPriority; @@ -145,6 +153,7 @@ public virtual void Stop() TH = null; MustExit = null; + mHandleArray = null; } } diff --git a/LaserGRBL/UserControls/GrblPanel3D.cs b/LaserGRBL/UserControls/GrblPanel3D.cs index 83132dbd..197c4816 100644 --- a/LaserGRBL/UserControls/GrblPanel3D.cs +++ b/LaserGRBL/UserControls/GrblPanel3D.cs @@ -75,7 +75,7 @@ public partial class GrblPanel3D : UserControl, IGrblPanel bool forcez = false; private bool mFSTrig; - private Base.Mathematics.MobileDAverageCalculator RenderTime; + private Base.Mathematics.MobileDAverageCalculator FrameTime; private Base.Mathematics.MobileDAverageCalculator RefreshRate; private static Exception FatalException; @@ -107,12 +107,13 @@ public static string GlDiagnosticMessage } } - Tools.ThreadObject TH = null; + AutoResetEvent RR = new AutoResetEvent(true); //redraw required + Tools.ThreadObject TH = null; //drawing thread public GrblPanel3D() { InitializeComponent(); OpCounter = 0; - RenderTime = new Base.Mathematics.MobileDAverageCalculator(30); + FrameTime = new Base.Mathematics.MobileDAverageCalculator(30); RefreshRate = new Base.Mathematics.MobileDAverageCalculator(30); mLastWPos = GPoint.Zero; mLastMPos = GPoint.Zero; @@ -136,7 +137,8 @@ public GrblPanel3D() mGrid = new Grid3D(); OnColorChange(); - TH = new Tools.ThreadObject(DrawScene, 10, true, "OpenGL", InitializeOpenGL, ThreadPriority.Lowest, ApartmentState.STA); + + TH = new Tools.ThreadObject(DrawScene, 100, true, "OpenGL", InitializeOpenGL, ThreadPriority.Lowest, ApartmentState.STA, RR); TH.Start(); } @@ -148,7 +150,12 @@ protected override void Dispose(bool disposing) { if (disposing && (components != null)) { - try { TH?.Stop(); TH.Dispose(); } catch { } + try + { + TH?.Stop(); + TH?.Dispose(); + TH = null; + } catch { } components.Dispose(); } base.Dispose(disposing); @@ -314,9 +321,9 @@ private void DrawScene() g.ReleaseHdc(handleDeviceContext); } mBmp.Bitmap = newBmp; - RenderTime.EnqueueNewSample(crono.ElapsedTime.TotalMilliseconds); + FrameTime.EnqueueNewSample(crono.ElapsedTime.TotalMilliseconds); - TH.SleepTime = BestSleep(RenderTime.Avg, 10, 100, 10, 50); + //TH.SleepTime = BestSleep(FrameTime.Avg, 10, 100, 10, 50); // call control invalidate Invalidate(); @@ -371,6 +378,8 @@ private void SetWorldPosition(double left, double right, double bottom, double t mCamera.Right = right; mCamera.Bottom = bottom; mCamera.Top = top; + + RR.Set(); } private void GrblPanel3D_Resize(object sender, EventArgs e) @@ -640,7 +649,7 @@ private void DoGDIDraw(PaintEventArgs e) else VertexString = string.Format("{0,6:###0.0} B", VertexCounter / 1000000000.0); - text = $"VER {VertexString}\nTIM {string.Format("{0,6:##0} ms", RenderTime.Avg)}\nFPS {string.Format("{0,6:##0}", 1000.0 / RefreshRate.Avg)}"; + text = $"VER {VertexString}\nAFT {string.Format("{0,6:##0} ms", FrameTime.Avg)}\nFPS {string.Format("{0,6:##0}", 1000.0 / RefreshRate.Avg)}"; size = MeasureText(text, font); point = new Point(Width - size.Width - mPadding.Right, pos); DrawOverlay(e, point, size, ColorScheme.PreviewRuler, 100); @@ -905,7 +914,7 @@ private void GrblPanel3D_Load(object sender, EventArgs e) private void TimDetectIssue_Tick(object sender, EventArgs e) { TimDetectIssue.Stop(); - if (OpCounter < 7 || FatalException != null) //non è riuscito a completare nemmeno due disegni nei 5 secondi del timer! (o se c'è stata una eccezione nei primi 5 secondi) + if (OpCounter < 5 || FatalException != null) //non è riuscito a completare nemmeno un disegno nei 5 secondi del timer! (o se c'è stata una eccezione nei primi 5 secondi) { if (FatalException == null) //fatal exception are logged where they are generated Logger.LogMessage("OpenGL", "Rendering issue detected! OpCounte: {0}", OpCounter);