Endless loop that doesn't break when you kill Dynamo

Using R21… I’ve been playing with windows forms.
I wrote a loop in the form (and obviously wrote it wrong)…
I got into an endless loop where my pop up keeps appearing.

I closed dynamo, the pop up is still going.

I close Revit - the pop up will not die!

I had to use task manager to kill the bloody thing!

Any idea why?

When you create a winform you’re creating a sub-process (endlessly) in the context of Revit.

While you may have closed the Revit window, that doesn’t mean the Revit process ends. You can do a standard shutdown of Revit and watch the list of things disappear from task manager over time as a test. Another test is to try and close Revit while Dynamo isn’t saved; I often get ‘no sign’ of Revit or civil 3d on any monitor while I have a popup asking me if I want to save an unsaved .dyn.

Because the thread of your window is either a child of the parent Revit process, or a part of the Revit process, while that code is executing Revit can’t shut down all the way. And because your winform never ends the parent process can never close completely. Every other aspect of the UI might be gone, but not the parent.

1 Like

You guys so need to add a break function :rofl:

Starting with a joking answer… Or when looking at it the other way… you guys need to add a break function to your winform. :stuck_out_tongue:

The serious answer though is this: Forcing loops to break by a cancel button is not something anyone wants. Stability and data fidelity are shot by introducing such a “feature”, and while 1% of users might want a ‘cancel’ button, 99% of users don’t want the data they’re working hard to produce to be corrupted (note that the 1% changed their mind once they were told what that would mean), and 110% of users don’t want applications to crash. So until that’s resolved (likely many many many many years from now) I’m fine with the disconnect. And I say this as someone who’d greatly benefit from that button.

Add a circuit breaker to your loops when testing. This can be done by using an and condition in your loop condition and then using += 1 to it on each iteration. If the breaker exceeds the counter, the and check fails and you’re safe.

Example (with ‘True’ being your main condition):

breaker = 0

while True and breaker < 5:
    breaker += 1

Likely a best practice in this case; I’ve seen lots of code bases with such an escape clause…


Well I always remember that AFTER. :rofl:

Never had one that keeps running after I’ve forced a shutdown with Dynamo and Revit before.

Any infinite or near infinite will do that.

The best lessons always have to be learnt the hard way. Precrash catching is one of those holy grails of programming, generally.

1 Like

Lessons are only worthwhile if I remember them…
I’ve messed up loops so many times I’m pretty sure that bit of storage in my brain is used for something more fun. :grin:

1 Like

Arrrgh! I inadvertently got stuck in a loop again…

I need a break function! It’s discriminatory not to give someone like me one. :rofl:

Ha! I think you yourself might be in an infinite loop! :joy:

Unfortunately, this is just a lesson you will have to learn (eventually! :grinning:)… Everyone learns the hard way with while loops, it’s usually that face-palm moment when you realise you forgot to add the break after 5 mins of running and you have to nuke your app with task manager. Do that enough times and you will remember…

Maybe a visual reminder on the wall next to your desk might help…

1 Like

always start them this way:

loopCount = 0
condition = False
while loopCount < 10 and not condition:
    condition = random.random() > 0.8 #ThingTo Do should go here
    loopCount +=1

Yeah but normally I’m asking it while true… not realising that i’m never going to hit true as I’ve written something wrong.

Just need to train yourself mentally to step carefully when using while loops. Comes with time.

That’s what my mum used to say about lots of I things I still can’t do :rofl:

1 Like

Actually I may make that into a custom node as a reminder. :smiley:

Better yet, put it into your Python template. :slight_smile: