Use Revit Transaction inside Async method Call

Use Revit Transaction inside Async method Call

Anonymous
Not applicable
3,182 Views
5 Replies
Message 1 of 6

Use Revit Transaction inside Async method Call

Anonymous
Not applicable

Hello Everyone, 
I am a Revit addins developer. One of my Winform c# addins, I am facing runtime Exception while implementing Async method. Below is the detail description of my issue

 

There is one method  which is call from Winform button click event. Inside that method I am implementing Revit API call to modify the document and for that I am using Revit Transaction. Now I want that method to be run in background so for that I have to make that method Async. When I convert that regular method to async, Revit document automatically set as read only inside that method and when I try to start the transaction it throws an exception. 

 

Exception thrown: 'Autodesk.Revit.Exceptions.InvalidOperationException' in RevitAPI.dll

Additional information: Cannot modify the document for either a read-only external command is being executed, or changes to the document are temporarily disabled.

 

Note: I am using Revit API 2015 dll and dot Net framework 4.5. Please suggest me the solution which is inside this scope.  

 

I also want to implement cancel button which stop the Async process any point of a time. I am appreciate if any of you suggest me the solution of this issue.  

0 Likes
Accepted solutions (1)
3,183 Views
5 Replies
Replies (5)
Message 2 of 6

SamBerk
Advocate
Advocate
Accepted solution

Hi @Anonymous 

 

Are you using IExternalEventHandler?

Have you set the TransactionMode to Manual?

 

[Transaction(TransactionMode.Manual)]
    public class ExternalEvent : IExternalEventHandler
    {
        public void Execute(UIApplication app)
        {
            // start transaction...
        }
    }

 

 

0 Likes
Message 3 of 6

Anonymous
Not applicable

I am not using IExternalEventHandler in this case 
Do you think should I use it which solve my problem ?
How can I Call IExternalEvent from Async method ?

0 Likes
Message 4 of 6

jeremytammik
Autodesk
Autodesk

You not only should, you must.

 

Look at the Revit SDK ExternalEvent sample and read The Building Coder articles on this topic:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 5 of 6

SamBerk
Advocate
Advocate

Hi @Anonymous 

 

According to my knowledge, I don't think it's possible to have an async transaction since you don't have control when the event should execute, you call event.Raise() which returns void, and when Revit is ready it will execute the event.

0 Likes
Message 6 of 6

Anonymous
Not applicable

@SamBerk  and @jeremytammik 
I called External Event from Async Method with event.Rise() so now no more exception related to Transaction but A new Exception raised which is related to PrintManager Access

Below is the line of code on which i am getting exception. This code is inside Transaction 
PrintManager pMgr = doc.PrintManager;  

 

Exception on this line is 

Exception thrown: 'Autodesk.Revit.Proxy.Exceptions.InvalidOperationExceptionProxy' in RevitDBAPI.dll

Additional information: Cannot access the paper size and paper source of current printer

right now this code line is inside External Event, I tried to put that code line outside the external Event and inside Async method but same exception

 

I am pretty sure that this exception due to either Async or Externel Event but don't know how to come out of it 

0 Likes