Stop an iLogic rule while it's running

Stop an iLogic rule while it's running

Daan_M
Collaborator Collaborator
2,726 Views
13 Replies
Message 1 of 14

Stop an iLogic rule while it's running

Daan_M
Collaborator
Collaborator

Hi,

 

I'm currently running a loop on 250+ files taking about 2hrs, i forgot one line of code.

 

Is it possible to cancel your rule during a run?  if so, how do i do this?

0 Likes
Accepted solutions (1)
2,727 Views
13 Replies
Replies (13)
Message 2 of 14

WCrihfield
Mentor
Mentor

I don't think that the folks at Autodesk have created a good solution for that situation yet.  It has been brought up and discussed before though, so it is definitely something on their radar.  Currently, just using Ctrl+Alt+Del keys to bring up the blue screen, then clicking Task manager, selecting the process, and using End Task.  Definitely not ideal though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 14

Daan_M
Collaborator
Collaborator

@WCrihfield Thanks, but forcing the program to shut down makes you loose the latest adjustments and takes alot of time starting up + opening files again.

 

Ofcourse the saving problem can be solved by saving the file everytime running code but it's easy to forget/quite annoying to have to do so. I could also put the safe as first thing to do in the code, but it's just a inefficient workaround when debugging code.

 

I see multiple other posts about this topic on the forum dating back all the way to 2011, in which an Autodesk employee actually responded and said they were planning on adding this functionality. I doubt they're going to do something about it to be honest, otherwise they would have done so in the past 12 years.

 

https://forums.autodesk.com/t5/inventor-programming-ilogic/how-to-stop-an-ilogic-loop-with-the-keybo...

 

Ofcourse no solution to the problem is also an answer, i'll work around it, thanks

Message 4 of 14

WCrihfield
Mentor
Mentor

I'm not holding my breath for it to happen either.  I know it is a pretty complex issue, but I'm not a software designer by trade, so I have no idea what it would take to implement something like that.  In the back of my mind, I'm thinking that we could maybe reference an external rule using AddVbFile, and within that rule have some sort of sophisticated custom event handler that is listening for a very specific System or Windows or Keyboard event to happen.  Then within the iLogic rule that is referencing that one, include some code just inside a potentially long code loop like that, that will exit that loop if that event is fired.  I have worked with several custom event handler codes before, but just from iLogic rules, since I lack the admin rights on my work PC to create my own add-ins (corporate security policies).  A different external rule could be used to launch the event handler when the first document gets opened in Inventor, then remains active until Inventor closes.  Just some thoughts rolling around in my mind on the topic.  If possible, it would still rely on us to insert the reference and needed code inside of our iLogic rule loop codes, so it would still not be as universal as we all want it to be.  But any step in that direction is better than none at all. 

Edit:  And if Inventor was still usable while the long loop was working, we might be able to use something like a SharedVariable.  Run another rule that changes the value of a SharedVariable, then within the large loop in the other rule, have some code near the start of the loop that checks the value of that SharedVariable, and if it has a specific value, exit the loop.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 14

Jacob__with__a__k
Enthusiast
Enthusiast
Accepted solution

Hi!

best way is indeed to check a certain variable every iteration of your loop.

Sharedvariables could work, but I personally don't like to use them.

 

the two ways I've implemented this are:

-checking if a certain file exists in the TEMP folder (just a txt-file with a certain name)

  code would look like this:

For Each ...
	If IO.File.exists("C:\Temp\IF_YOU_DELETE_THIS_CODE_WILL_STOP.txt")
                MsgBox("rule " & iLogicVb.RuleName & " stopped because text file doesn't exist")
'logger.info("rule " & iLogicVb.RuleName & " stopped because text file doesn't exist")
return
'exit sub
'goto ... End If ... Next

deleting or renaming txt file is enough to stop the code (next iteration)

 

-you can also use the progressbar "cancel" button to stop the loop

https://forums.autodesk.com/t5/inventor-programming-ilogic/using-progressbar-oncancel-event/td-p/587...

 

Public Sub Main
    progBar = New TestProgressBar(ThisApplication)
    progBar.Start()
End Sub

Public Class TestProgressBar
    Private WithEvents progBar As Inventor.ProgressBar
    Private invApp As Inventor.Application

    Public Sub New(InventorApp As Inventor.Application)
        invApp = InventorApp
    End Sub

    Public Sub Start()
        progBar = invApp.CreateProgressBar(False, 10, "Test of Progress Bar", True)

        Dim j As Integer
        For j = 0 To 10
            progBar.Message = ("Current Index: " & j & "/" & 10)
            Threading.Thread.Sleep(1000)
            progBar.UpdateProgress()
        Next

        progBar.Close()
    End Sub

    Private Sub progBar_OnCancel() Handles progBar.OnCancel
        progBar.Close()
        MsgBox("Cancelled")
    End Sub
End Class

 should work the same way: click cancel and next iteration the code stops

 

Happy coding!

Message 6 of 14

WCrihfield
Mentor
Mentor

Good idea.  However, the Inventor.ProgressBar (API Sample Here) has the same important limitation as the SharedVariable idea...if Inventor is frozen at the time, due to the long running process, we would not be able to make the needed change.  The idea about checking for the existence of a text file, even though it is the same strategy as SharedVariable.Exists, would likely be more reliable if Inventor was frozen at the time, since that is something that can be changed externally of Inventor.  I have used the SharedVariable.Exists method to exit ongoing processes several times myself over the past couple years without any problems, but Inventor was not frozen at the time either.  I use that strategy sometimes to exit custom event handlers that are running in the background.  Every time the event is triggered, it checks for the SharedVariable first, before doing anything else.  If it either does not exist, or it does exist but has a False value, the event handler gets removed before reacting to the event.  If it does exist, and has a True value, the event handler keeps functioning as designed.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 14

Jacob__with__a__k
Enthusiast
Enthusiast
(don't quote me on this, not tested it) but since since Inventor.Progressbar runs on a different thread (so I'm told) it might work without problem in those situations where the rest of Inventor freezes

I would love to get some pointers about custom event handlers, if you have some
never used them, but it sounds like they could be very useful
since I've only coded in Ilogic (nothing in vba except excel) I wouldn't have a clue where to begin
0 Likes
Message 8 of 14

WCrihfield
Mentor
Mentor

I appreciate the added note.  I had not heard anything about the ProgressBar running on another thread, but like you, I am not 100% sure if it might be true because I do not recall ever personally testing that specific functionality of it being able to stop a process in Inventor that was causing it to freeze up.  If that is true, it would certainly be nice.  I still would not be able to use a ProgressBar to stop just one of multiple event handlers that are running in the background through, because they do not really make 'progress' or use a loop, they just react to a specific event when it happens.  I have seen an example or two of laying out different code routines in a way that would cause Inventor to do two (or more) things at the same time on different threads, to help improve performance, but I have not delved that deep into it myself before.  In the past, I had heard that Inventor was not that good at utilizing multi-threading functionality, but in more recent years I have heard that it is getting better.  I just generally do not use iLogic for tasks that are going to take a super long time to process while I am still present at my computer.

 

As for how to create and use custom event handlers...although we can create and use these from iLogic rules, they are generally best suited for Inventor add-ins.  A couple years ago I had an knowledge base article published on the knowledge.autodesk.com/community/ domain about that topic, but they took all user submitted articles down about a year ago.  I have attached a PDF of what remains of that article.  Within that article you will find the two main ways to deal with them, a bunch of links to some of Inventor's events that you can listen for, and a brief example iLogic rule which uses a couple of them.  That example is not a really good one, and there are a several more events available now than there were then, but its at least a starting point.  Each event has its own 'handler' Sub routine definition line that was designed by the folks at Autodesk, which we utilize as needed.  Then we can put our own custom code within the routine, for how to react to the event.  There are a few main precautions about creating event handlers with iLogic rules to keep in mind.  Once you start one, depending on its scope, it will most likely keep running in the background until you close Inventor, unless you incorporate something into the rule that creates it, to remove it at some point.  You can not simply change the code within the rule that launched it after it has been launched to change its functionality, because that original code is then being held in Inventor's session memory while it is running.  So, in some early event handler testing, you may have to close Inventor to get rid of the one that is already running, unless you make them a 'one shot' routine.  In a one shot routine, you just remove the handler at the end of the Sub routine that reacts to the event the first time.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 14

Jacob__with__a__k
Enthusiast
Enthusiast
Thank you for the quick response, all the information and for sharing your article!
I'll definitly give the event handlers and custom add-ins a shot!
0 Likes
Message 10 of 14

JelteDeJong
Mentor
Mentor

I wrote a blog article on this topic. It uses the Progressbar to stop you actions. You might want to have a look at my Post:
http://hjalte.nl/64-stop-long-running-rule 
And if there is a follow up article that could be interesting as well:
http://hjalte.nl/65-undo 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 11 of 14

Stakin
Collaborator
Collaborator

@JelteDeJong @WCrihfield Is there a way to stop the ilogic running by open a new inventor application  to handle it?

0 Likes
Message 12 of 14

WCrihfield
Mentor
Mentor

Hi @Stakin.  I honestly don't know if that would be possible, but it does not sound likely to me.  But I am not a seasoned software developer, so I certainly don't know everything there is to know about dealing with these types of situations entirely by code.

I know that we can access the 'Process' that is handling that running instance of Inventor that is not responding (Process.GetProcessesByName), and that we can attempt to 'Close' or 'Kill' that Process.  Kill is similar to manually launching the Task Manager, right-clicking on it and clicking End Task, which is certainly not ideal.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 13 of 14

Stakin
Collaborator
Collaborator

I don't know how to program, I thought it maybe could access and manipulate his internal processes with the process that got from the system, thanks.

The ilogic program of the inventor process I worked on today has been running non-stop, and the program debugging information has been constantly output, not that the inventor process is not responding. The reason is that I made a mistake with a single line of code that caused it to loop through more than a thousand-part assembly, roughly estimated to be at least 1000 cycles to the power of 1000, and has been running for more than 8 hours at the moment, but I don't want to end the process because there are some important changes that are not saved. Hopefully it'll be able to finish tomorrow morning.

0 Likes
Message 14 of 14

WCrihfield
Mentor
Mentor

If Inventor is not frozen, and you are still able to manually interact with it, then there may be a way to stop the iLogic rule.  Since iLogic is an application add-in (ApplicationAddIn) for Inventor, there is something you can try, but it may not work either.  On the Tools tab, Options panel, click on the Add-ins button, which will open a small dialog.  In that dialog are two tabs at the top.  Activate the one named 'Applications'.  Scroll the list and select the add-in named 'iLogic'.  Then see if it will let you un-check the checkbox next to the control named 'Loaded/Unloaded'.  Sometimes that control is greyed out though.  That control loosely coincides with the ApplicationAddIn.Activate method and ApplicationAddIn.Deactivate method, on the Inventor API side.  If it lets you, and the code gets stopped, I don't know what all will happen though.  The work it has already done will likely remain changed, but will also likely remain unsaved, but you may be able to manually save any visibly open documents that need to be saved, which may also save any other documents that they reference, if they have been 'dirtied' (savable changes since last saved).

I have definitely been in similar situations before myself, but did not have the patience to wait that long for it to finish.  So usually either I 'end tasked' the application's process in the task manager, or Inventor crashed.  I learned to be more careful about potentially endless loops in my code, or potentially very long running processes, and eventually started using the Inventor.ProgressBar in the ones that may take a long time to process, such as ones working on a lot of files.  It is not always simple to set-up and get working properly though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes