Modeless Crashing

Modeless Crashing

PaulCollins7972
Advocate Advocate
706 Views
3 Replies
Message 1 of 4

Modeless Crashing

PaulCollins7972
Advocate
Advocate

I have an Addin based on the Modeless Sample which our staff use to manage sheets, including creating PDF's by printing to the Adobe PDF driver.

For most of the time and with most of the users it all works fine. However there have been several cases of it causing Revit to crash when printing. I have had this while debuging, it happens at the SubmitPrint line

 

Here is my code for printing selected sheets:

 

Public Sub SelectionPrint(ByVal uiapp As UIApplication)
Try
Dim doc As Autodesk.Revit.DB.Document = uiapp.ActiveUIDocument.Document
Dim uidoc As UIDocument = uiapp.ActiveUIDocument

 

For n = 0 To Selected_ID_List.Count - 1
Dim vw As Autodesk.Revit.DB.View = doc.GetElement(Selected_ID_List(n))

 

'open the view and then print it
uidoc.ActiveView = vw

 

'print the active view
MyPrintMgr.SubmitPrint()


Next

 

Select Case Selected_ID_List.Count
Case 1
Update_Text = "1 sheet printed"
Case Else
Update_Text = Selected_ID_List.Count & " sheets printed"
End Select

 

Catch ex As Exception
DisplayError(ex)
End Try
End Sub

 

I recently came across this article by Aaron Lu and wondered if something similar is happening?

 

Edited on 2016/3/8

Someone found the above code "ExternalEvent.Create" method throws Autodesk.Revit.Exceptions.InvalidOperationException: Attempting to create an ExternalEvent outside of a standard API execution, that is a true bug in my code, sorry for that :-(. And the solution is easy: do not call "ExternalEvent.Create" in modeless dialog but in "IExternalCommand.Execute" or "IExternalApplication.OnStartup". Following is a full code example showing that: Run ExternalCommand "EventRegistrationInModelessDialogViaExternalEvent" and then click the button in the modeless dialog to register or unregister DocumentChanged event.

 

http://adndevblog.typepad.com/aec/2015/09/revitapi-revit-2016-events-registerunregister-behavior-cha...

 

0 Likes
707 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

Hi Paul, 

the post on ADN-devblog is referring to when you try to create an ExternalEvent out of correct Revit context. 
That is correct, you shouldnt, from your code I cant see if that is the case since only the print method is shown. 
But that should give you an exception every time you try to invoke ExternalEvent.Create() not every now and then if you have a modeless window. 

Furthermore, in my experience, wrapping your code in try-catch prevents revit from doing a hard crash on exceptions. So Im guessing the problem might be somewhere else. 

To prevent hard crashes with modeless dialogs I suggest wrapping as much of your code as possible in try-catch blocks and log the exceptiondump to a file and show the user that something has happend. 

 

 

0 Likes
Message 3 of 4

PaulCollins7972
Advocate
Advocate

Hi Erik

 

I gave up with trying to make my Add-in work as modeless and reverted to the modal version as there were too many issues.

 

Not sure that I will try again as I spent many hours coding only to find that the modeless technique was unreliable for me.

 

I had try-catch around most if not all of the code but still it crashed Revit not just the Add-in

 

Paul

0 Likes
Message 4 of 4

Anonymous
Not applicable

Hi Paul, 

 

sorry to hear that. 
I dont know what IDE you are using, but did you attach the debugger to see where the exceptions where thrown? Visual Studio will catch the exceptions before they are returned to Revit and you can have a look at the call stack and other important state. 

/Erik

0 Likes