Skip to content

Commit fbb0336

Browse files
committed
routine
1 parent 5bfed47 commit fbb0336

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

GH_Ghost/Ghost.cs

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Rhino;
1515
using Rhino.NodeInCode;
1616
using GH_IO.Serialization;
17+
using System.Windows.Forms;
1718

1819
// In order to load the result of this wizard, you will also need to
1920
// add the output bin/ folder of this project to the list of loaded
@@ -38,16 +39,17 @@ public Ghost()
3839
{
3940
}
4041

41-
protected bool complete = false; //TODO: serialize these
42+
protected bool complete = false;
4243
protected bool running = false;
4344
protected bool interrupt = false;
44-
protected long comptime;
45-
protected string[] workerwarnings = new string[] { };
45+
protected bool trigger = false;
4646

47+
protected string comptime;
48+
protected string[] workerwarnings = new string[] { };
4749
protected bool reparams = false;
4850
protected ComponentFunctionInfo trgtcomp;
4951
protected IGH_DocumentObject srcobj;
50-
protected Guid srcobj_id = Guid.Empty;
52+
protected Guid srcobj_id = Guid.Empty; // for IO only
5153
protected object[] evaluated;
5254

5355
/// <summary>
@@ -102,12 +104,13 @@ protected void GhostEval(object[] prms)
102104
if (prms.Length == 0 || trgtcomp==null)
103105
{
104106
complete = true && !interrupt;
105-
lock (locker) comptime = 0;
107+
lock (locker) comptime = " 0ms";
106108
}
107109
else if (running)
108110
{
109111
// shouldn't ever be here!
110112
complete = false;
113+
lock (locker) workerwarnings = new string[] { "Somehow a second ghost tried to start a task. It's been stopped.", };
111114
return; //skip invoke recompute
112115
}
113116
else
@@ -125,13 +128,49 @@ protected void GhostEval(object[] prms)
125128
ticker.Stop();
126129
lock (locker)
127130
{
128-
comptime = ticker.ElapsedMilliseconds;
131+
comptime = WatchParse(ticker);
129132
evaluated = results;
130133
}
131134
}
132135
RhinoApp.InvokeOnUiThread(new Action<bool>(ExpireSolution), new object[] { true, });
133136
}
134-
137+
/// <summary>
138+
/// parse the stopwatch ellapsed time
139+
/// </summary>
140+
/// <param name="w">stop watch object</param>
141+
/// <returns>legible string of the time span</returns>
142+
protected string WatchParse(Stopwatch w)
143+
{
144+
if (w.ElapsedMilliseconds < 1000)
145+
return string.Format(" {0}ms", w.ElapsedMilliseconds);
146+
else
147+
{
148+
var h = w.Elapsed.Hours;
149+
var m = w.Elapsed.Minutes;
150+
var s = w.Elapsed.Seconds;
151+
var ms = w.Elapsed.Milliseconds;
152+
return string.Format(" {0}{1}{2}",
153+
h == 0 ? "" : h.ToString() + "hrs ",
154+
m == 0 ? "" : m.ToString() + "mins ",
155+
s == 0 ? "" : (s+ms/1000.0).ToString() + "secs"
156+
);
157+
}
158+
}
159+
160+
private void OnUnblock(object s, EventArgs e)
161+
{
162+
trigger = !trigger;
163+
}
164+
public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
165+
{
166+
base.AppendAdditionalMenuItems(menu);
167+
try
168+
{
169+
ToolStripMenuItem recomp = menu.Items.Add("Unblock recompute queueing", null, OnUnblock) as ToolStripMenuItem;
170+
recomp.Checked = trigger;
171+
}
172+
catch { }
173+
}
135174

136175
/// <summary>
137176
/// Registers all the input parameters for this component.
@@ -243,7 +282,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
243282
complete = false; // sets up next solution
244283
Message = "Finished";
245284
consoletxt += string.Format(
246-
comptime == 0 ? "\n(that thing computed instantly...you really needed me here?)" : "\n(latest solution took {0}ms)",
285+
comptime == " 0ms" ? "\n(that thing computed instantly...you really needed me here?)" : "\n(latest solution took{0})",
247286
comptime);
248287
try { consoletxt += "\nRuntime warnings >>>\n" + string.Join("\n", workerwarnings); }
249288
catch (ArgumentNullException) { }
@@ -252,8 +291,13 @@ protected override void SolveInstance(IGH_DataAccess DA)
252291
else if (running && !complete)
253292
{
254293
Message = "Computing";
255-
interrupt = true;
256-
consoletxt += "\n(interruption detected\nsolution will restart once current task finishes)";
294+
if (trigger)
295+
{
296+
interrupt = true;
297+
consoletxt += "\n(interruption detected\nsolution will restart once current task finishes)";
298+
}
299+
else
300+
consoletxt += "\n(interruption not handled\n unblock queueing in context menu if needed)";
257301
}
258302
else
259303
{
@@ -280,7 +324,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
280324
}
281325
if (evaluated[i - 1] is IGH_DataTree outtree)
282326
DA.SetDataTree(i, outtree);
283-
else
327+
else if (!running)
284328
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, " internal error: worker output data mismatch");
285329
}
286330
}
@@ -299,7 +343,7 @@ public bool CanInsertParameter(GH_ParameterSide side, int i)
299343
{
300344
if (side == GH_ParameterSide.Input && i <= comp.Params.Input.Count && i == Params.Input.Count)
301345
return true;
302-
else if (side == GH_ParameterSide.Output && i <= comp.Params.Input.Count &&i == Params.Output.Count)
346+
else if (side == GH_ParameterSide.Output && i <= comp.Params.Output.Count && i == Params.Output.Count)
303347
return true;
304348
else
305349
return false;

0 commit comments

Comments
 (0)