Skip to content

Commit

Permalink
Drawing Thread is now driven by request
Browse files Browse the repository at this point in the history
  • Loading branch information
arkypita committed Jun 16, 2024
1 parent 5ddb1ed commit 168082c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
4 changes: 2 additions & 2 deletions LaserGRBL/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public partial class MainForm : Form

public MainForm()
{
ColorScheme.CurrentScheme = Settings.GetObject("Color Schema", ColorScheme.Scheme.CADDark);

InitializeComponent();
ExceptionManager.ParentMain = this;

Expand Down Expand Up @@ -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");
Expand Down
21 changes: 15 additions & 6 deletions LaserGRBL/Tools/ThreadClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,26 +43,31 @@ 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;
}


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
}


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -145,6 +153,7 @@ public virtual void Stop()

TH = null;
MustExit = null;
mHandleArray = null;
}
}

Expand Down
27 changes: 18 additions & 9 deletions LaserGRBL/UserControls/GrblPanel3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}

Expand All @@ -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);
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 168082c

Please sign in to comment.