Timer inside component - Grasshopper2024-03-28T09:45:49Zhttps://www.grasshopper3d.com/forum/topics/timer-inside-component?feed=yes&xn_auth=noIn those cases when grasshopp…tag:www.grasshopper3d.com,2017-06-11:2985220:Comment:17675832017-06-11T20:32:33.878ZPetras Vestartashttps://www.grasshopper3d.com/profile/PetrasVestartas
<p>In those cases when grasshopper is unloaded, component is deleted and if document is inactive is there an easy way to check this and dispose the timer?</p>
<p><span> </span></p>
<p><span> </span></p>
<p>In those cases when grasshopper is unloaded, component is deleted and if document is inactive is there an easy way to check this and dispose the timer?</p>
<p><span> </span></p>
<p><span> </span></p> Do you mean if grasshopper cr…tag:www.grasshopper3d.com,2017-06-11:2985220:Comment:17678632017-06-11T20:23:17.618ZDavid Ruttenhttps://www.grasshopper3d.com/profile/DavidRutten
<blockquote><p><span>Do you mean if grasshopper crashes or I simply close rhino?</span></p>
</blockquote>
<p></p>
<p>No in those cases the timer will be properly destroyed as well. I mean when the grasshopper document that contains the timer is closed (but Grasshopper remains active) or when the document is deactivated (because a different document is loaded into the canvas) or when the component is deleted.</p>
<p></p>
<p>It sort of depends on whether your timer needs to keep checking stuff…</p>
<blockquote><p><span>Do you mean if grasshopper crashes or I simply close rhino?</span></p>
</blockquote>
<p></p>
<p>No in those cases the timer will be properly destroyed as well. I mean when the grasshopper document that contains the timer is closed (but Grasshopper remains active) or when the document is deactivated (because a different document is loaded into the canvas) or when the component is deleted.</p>
<p></p>
<p>It sort of depends on whether your timer needs to keep checking stuff even if the document is inactive. But if the document is unloaded or if the component is deleted you definitely want to disable and dispose the timer.</p> Yes I added this in visual st…tag:www.grasshopper3d.com,2017-06-11:2985220:Comment:17675802017-06-11T19:34:51.521ZPetras Vestartashttps://www.grasshopper3d.com/profile/PetrasVestartas
<p>Yes I added this in visual studio and it works as a charm:</p>
<p></p>
<p><em>private void TimerElapsed(object sender, EventArgs e)</em><br></br> <em>{</em><br></br> <em>// First check to see if the current minute is the same as the previous minute.</em><br></br> <em>DateTime now = DateTime.Now;</em><br></br> <em>// if (_minute == now.Minute) return;</em><br></br> <em>if(Running == false || count2 > frameRange.T1 || VRayInterface.HasRenderFinished() == false)</em><br></br> <em>return;</em></p>
<p><em>// If not,…</em></p>
<p>Yes I added this in visual studio and it works as a charm:</p>
<p></p>
<p><em>private void TimerElapsed(object sender, EventArgs e)</em><br/> <em>{</em><br/> <em>// First check to see if the current minute is the same as the previous minute.</em><br/> <em>DateTime now = DateTime.Now;</em><br/> <em>// if (_minute == now.Minute) return;</em><br/> <em>if(Running == false || count2 > frameRange.T1 || VRayInterface.HasRenderFinished() == false)</em><br/> <em>return;</em></p>
<p><em>// If not, invoke the ExpireSolution method on the UI thread.</em><br/> <em>// To do so, we must get access to a UI control. On Rhino6 you can use Rhino.RhinoApp.InvokeOnUiThread().</em><br/> <em>System.Windows.Forms.Control control = Grasshopper.Instances.ActiveCanvas;</em><br/> <em>if (control == null)</em><br/> <em>return;</em><br/> <br/> <em>Action<bool> action = new Action<bool>(base.ExpireSolution);</em><br/> <em>control.Invoke(action, true);</em><br/> <em>}</em></p>
<p></p>
<p>Thank you very very much:)</p>
<p></p>
<p></p>
<p>Regarding:</p>
<p>"<strong><em>There's no smarts there at all to disable the timer if the document goes inactive or is unloaded, you'll have to add that sort of bookkeeping.</em></strong>"</p>
<p></p>
<p>Do you mean if grasshopper crashes or I simply close rhino?</p>
<p>What would be the way to turn it off? Should I add some sort of boolean if current grasshopper document is open?</p>
<p></p>
<p></p> See attached. It contains a C…tag:www.grasshopper3d.com,2017-06-11:2985220:Comment:17676802017-06-11T19:07:43.617ZDavid Ruttenhttps://www.grasshopper3d.com/profile/DavidRutten
<p>See attached. It contains a C# component with a custom timer which fires every quarter second. However it only triggers an update if the current minute is different from the minute the previous solution ran in.</p>
<p></p>
<p>There's no smarts there at all to disable the timer if the document goes inactive or is unloaded, you'll have to add that sort of bookkeeping.</p>
<p>See attached. It contains a C# component with a custom timer which fires every quarter second. However it only triggers an update if the current minute is different from the minute the previous solution ran in.</p>
<p></p>
<p>There's no smarts there at all to disable the timer if the document goes inactive or is unloaded, you'll have to add that sort of bookkeeping.</p> In the end it is all related…tag:www.grasshopper3d.com,2017-06-11:2985220:Comment:17678332017-06-11T14:48:04.822ZPetras Vestartashttps://www.grasshopper3d.com/profile/PetrasVestartas
<p>In the end it is all related to one single component made.</p>
<p></p>
<p>If you could show me an example about timer events raised on a separate thread I would be very thankful:) </p>
<p>In the end it is all related to one single component made.</p>
<p></p>
<p>If you could show me an example about timer events raised on a separate thread I would be very thankful:) </p> Yes, your best solution is to…tag:www.grasshopper3d.com,2017-06-11:2985220:Comment:17678172017-06-11T11:08:36.971ZDavid Ruttenhttps://www.grasshopper3d.com/profile/DavidRutten
<p>Yes, your best solution is to use a regular timer (provided by .NET framework, System.threading namespace). Have this timer fire events every half a second or so (or however often you feel is reasonable). Every time the timer fires you check the completeness of the rendering. If it completed, you then expire everything you need to expire and trigger a new solution. If it's just a single component, then <em>comp.Expiresolution(true)</em> is the best way to do this.</p>
<p></p>
<p>BUT BE…</p>
<p>Yes, your best solution is to use a regular timer (provided by .NET framework, System.threading namespace). Have this timer fire events every half a second or so (or however often you feel is reasonable). Every time the timer fires you check the completeness of the rendering. If it completed, you then expire everything you need to expire and trigger a new solution. If it's just a single component, then <em>comp.Expiresolution(true)</em> is the best way to do this.</p>
<p></p>
<p>BUT BE CAREFUL!!!! Timer events will be raised on a separate thread. You cannot start new solutions on anything except the Grasshopper/Rhino UI thread. This means you must Invoke the relevant methods via the UI thread. If you don't know how to do this, I can set up an example.</p> Is there a way to do the foll…tag:www.grasshopper3d.com,2017-06-11:2985220:Comment:17676342017-06-11T09:53:58.585ZPetras Vestartashttps://www.grasshopper3d.com/profile/PetrasVestartas
<p>Is there a way to do the following then:</p>
<p>I am checking if render is finished (from vray sdk), if it is finished I would like to recompute the solution. But if I just add this boolean property it just checks once, so I thought it would be good to check constantly and not to expire solution if it is still rendering. But it seems that after expire solution I cannot stop anything.</p>
<p>(for animations I check if render is finished, if it is finished recompute solution to update…</p>
<p>Is there a way to do the following then:</p>
<p>I am checking if render is finished (from vray sdk), if it is finished I would like to recompute the solution. But if I just add this boolean property it just checks once, so I thought it would be good to check constantly and not to expire solution if it is still rendering. But it seems that after expire solution I cannot stop anything.</p>
<p>(for animations I check if render is finished, if it is finished recompute solution to update grasshopper definition) </p> Anything inside SolveInstance…tag:www.grasshopper3d.com,2017-06-11:2985220:Comment:17676172017-06-11T04:51:09.770ZDavid Ruttenhttps://www.grasshopper3d.com/profile/DavidRutten
<p>Anything inside SolveInstance is called well after the expiration shockwave has made its way through the network and thus cannot be used to prevent a recalculation. It's simply too late at this point. The <em>only</em> way to stop components from recollecting and recalculating <em>and keep their old data</em>, is to <em>not</em> call ExpireSolution(bool).</p>
<p>Anything inside SolveInstance is called well after the expiration shockwave has made its way through the network and thus cannot be used to prevent a recalculation. It's simply too late at this point. The <em>only</em> way to stop components from recollecting and recalculating <em>and keep their old data</em>, is to <em>not</em> call ExpireSolution(bool).</p> I think you might find the IG…tag:www.grasshopper3d.com,2017-06-10:2985220:Comment:17674592017-06-10T22:23:41.791ZMateusz Zwierzyckihttps://www.grasshopper3d.com/profile/MateuszZwierzycki
<p>I think you might find the <a rel="nofollow" href="http://developer.rhino3d.com/wip/api/grasshopper/html/M_Grasshopper_Kernel_IGH_DataAccess_AbortComponentSolution.htm" target="_blank">IGH_DataAccess.AbortComponentSolution</a> method an interesting option in your case, which you can call from the SolveInstance <em>DA</em> argument.</p>
<p>I think you might find the <a rel="nofollow" href="http://developer.rhino3d.com/wip/api/grasshopper/html/M_Grasshopper_Kernel_IGH_DataAccess_AbortComponentSolution.htm" target="_blank">IGH_DataAccess.AbortComponentSolution</a> method an interesting option in your case, which you can call from the SolveInstance <em>DA</em> argument.</p> If you do not want a recomput…tag:www.grasshopper3d.com,2017-06-08:2985220:Comment:17662662017-06-08T17:44:21.819ZDavid Ruttenhttps://www.grasshopper3d.com/profile/DavidRutten
<p>If you do not want a recomputation, you must not expire the component. Thus, the logic which decides whether or not to update must be inside the <em>ScheduleCallback</em> method.</p>
<p>If you do not want a recomputation, you must not expire the component. Thus, the logic which decides whether or not to update must be inside the <em>ScheduleCallback</em> method.</p>