Is it possible to create an ILogic Rule within an Assembly to close it?

Is it possible to create an ILogic Rule within an Assembly to close it?

DSilver4773
Advocate Advocate
731 Views
5 Replies
Message 1 of 6

Is it possible to create an ILogic Rule within an Assembly to close it?

DSilver4773
Advocate
Advocate

I have an assembly with ilogic code that manipulates the assembly, when the manipulation is done i would like to be able to close the assembly. When I try this with

ThisDoc.Document.Close(True)

The file closes but then flags a catastrophic error.  Is this because the rule doesn't like to be run from within the assembly?

0 Likes
Accepted solutions (1)
732 Views
5 Replies
Replies (5)
Message 2 of 6

JhoelForshav
Mentor
Mentor

Hi @DSilver4773 

You are right. The rule runs from within the document and this error occurs because the document closes before the rule has run to completion. So short answear is that It's not possible.

 

I'd suggest using an external rule for this.

0 Likes
Message 3 of 6

DRoam
Mentor
Mentor

Hi @DSilver4773, I just discovered a workaround for this that should work. It did when I tested it; you'll have to try it with your own rule and let me know if it works. Basically, you close the document by starting a new thread which instructs the document to close. Because it takes place in a different thread, the catastrophic error doesn't occur. Here's how to implement it:

 

(IMPORTANT EDIT: I did some testing after my original post and remembered that iLogic parameters whose value you set using "<Parameter Name> = <Value>" (where the parameter name shows up in blue) aren't actually written with the new values until the rule finishes execution. However, the "CloseDocument" thread causes the rule to exit prematurely, so this doesn't happen. So if you use the "blue parameters" method to modify parameters, you'll need to force those values to be written by calling "RuleParametersOutput", as I do below).

 

 

Sub Main()
	' <your rule's code here>
	' ...
	
	' IMPORTANT: INCLUDE THESE LINES IF YOU WANT YOUR CHANGES TO BE SAVED.
	RuleParametersOutput
	ThisDoc.Save
	
	' Close Document.
	Call New System.Threading.Thread(AddressOf CloseDocument).Start
End Sub

Sub CloseDocument
	ThisDoc.Document.Close(True)
End Sub

 

Message 4 of 6

DSilver4773
Advocate
Advocate

Your suggestion worked great!

Many Thanks

Dan

0 Likes
Message 5 of 6

DRoam
Mentor
Mentor
Accepted solution

Just wanted to come back and mention that I realized you can call the "Close" method as a lambda sub rather than having to have it in a separate sub, which simplifies things a bit. So the "close myself" code can be simplified to this:

 

(IMPORTANT NOTE, repeated: iLogic parameters whose value you set using "<Parameter Name> = <Value>" (where the parameter name shows up in blue) aren't actually written with the new values until the rule finishes execution. However, the "close document" thread causes the rule to exit prematurely, so this doesn't happen. So if you use the "blue parameters" method to modify parameters, you'll need to force those values to be written by calling "RuleParametersOutput" and "ThisDoc.Save" before closing the document, as I do below).

 

' <your rule's code here>
' ...

' IMPORTANT: INCLUDE THESE LINES IF YOU WANT YOUR CHANGES TO BE SAVED.
RuleParametersOutput
ThisDoc.Save

' Close Document.
Call New System.Threading.Thread(Sub() ThisDoc.Document.Close(True)).Start

 

Message 6 of 6

DRoam
Mentor
Mentor

@DSilver4773 ,if you could un-mark my original answer as the solution, and mark the previous one instead, so that it's the one people see first, that would be great 🙂