Why is the Autodesk.Revit.Exceptions.InternalException thrown?

Why is the Autodesk.Revit.Exceptions.InternalException thrown?

Anonymous
Not applicable
1,380 Views
2 Replies
Message 1 of 3

Why is the Autodesk.Revit.Exceptions.InternalException thrown?

Anonymous
Not applicable

Hello.
In the process, an exception was thrown.


Autodesk.Revit.Exceptions.InternalException:
'A managed exception was thrown by Revit or by one of its external applications.'

 

Whenever the combo box item is changed, the source below is processed.

 

 

var rooms = new System.Collections.Concurrent.ConcurrentBag<RevitRoomModel>();
new FilteredElementCollector(Document).WhereElementIsNotElementType()
    .WherePasses(new ElementCategoryFilter(BuiltInCategory.OST_Rooms))
    .Cast<Room>()
    .Where(x => x.Area > 0 && x.LevelId.ToString().Equals(selectedLevel.LevelId))
    .AsParallel()
    .ForAll(x =>
    {
        if (x.GetParameters($"RoomProperty").FirstOrDefault() is Parameter parameter)
        {
            var value = parameter.StorageType.Equals(StorageType.String) ? parameter.AsString() : parameter.AsValueString();
            if (string.IsNullOrWhiteSpace(value) == false || string.IsNullOrEmpty(value) == false)
            {
                rooms.Add(new RevitRoomModel { Element = x, RoomProperty = value });
            }
            else
            {
                rooms.Add(new RevitRoomModel { Element = x, RoomProperty = value });
            }
        }
        else
        {
            rooms.Add(new RevitRoomModel { Element = x, RoomProperty = "x" });
        }
    });


var wallsRun = new System.Collections.Concurrent.ConcurrentBag<RevitWallModel>();
new FilteredElementCollector(Document).WhereElementIsNotElementType()
    .WherePasses(new ElementClassFilter(typeof(Wall)))
    .Cast<Wall>()
    .Where(x => x.LevelId.ToString().Equals(selectedLevel.LevelId))
    .AsParallel()
    .ForAll(x => wallsRun.Add(new RevitWallModel { Element = x }));
foreach (var wall in wallsRun)
{
    if (wall.Element.GetParameters($"WallProperty").FirstOrDefault() is Parameter parameter)
    {
        var value = parameter.StorageType.Equals(StorageType.String) ? parameter.AsString() : parameter.AsValueString();
        if (string.IsNullOrWhiteSpace(value) == false || string.IsNullOrEmpty(value) == false)
        {
            wall.WallProperty = value;
        }
        else
        {
            wall.WallProperty = value;
        }
    }
    else
    {
        wall.WallProperty = "x";
    }
}


var wallsWrong = new System.Collections.Concurrent.ConcurrentBag<RevitWallModel>();
new FilteredElementCollector(Document).WhereElementIsNotElementType()
   .WherePasses(new ElementClassFilter(typeof(Wall)))
   .Cast<Wall>()
   .Where(x => x.LevelId.ToString().Equals(selectedLevel.LevelId))
   .AsParallel()
   .ForAll(x =>
   {
       if (x.GetParameters($"WallProperty").FirstOrDefault() is Parameter parameter) // ◀ ※ InternalException ※
       {
           var value = parameter.StorageType.Equals(StorageType.String) ? parameter.AsString() : parameter.AsValueString();
           if (string.IsNullOrWhiteSpace(value) == false || string.IsNullOrEmpty(value) == false)
           {
               wallsWrong.Add(new RevitWallModel { Element = x, WallProperty = value });
           }
           else
           {
               wallsWrong.Add(new RevitWallModel { Element = x, WallProperty = value });
           }
       }
       else
       {
           wallsWrong.Add(new RevitWallModel { Element = x, WallProperty = "x" });
       }
   });

 

 

 

The first 'Room' works fine.
In the case of Room and 'wallWrong', the logic is the same, but an exception was thrown.

(The room works fine and the wall fails. Why!?)

 

Please take a look at the second 'wallRun' and the third 'wallWrong' case.

It is subtly different and works fine in the case of 'wallsRun', but an exception was throws in the case of 'wallsWrong'.

 

Here I don't think I need a transaction. It just loads the parameters and checks them.
Why is this exception was thrown and is there a solution?

 

 

Thanks.

0 Likes
Accepted solutions (1)
1,381 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni
Accepted solution

AsParallel enables the following code to be run in several simultaneous parallel threads:

 

 

The Revit API is limited to a single thread, the main thread:

 

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

rhanzlick
Advocate
Advocate

@jeremy_tammik, Thanks to your many blog posts, I have understood for years of Revit's aversion to multithreading. However, upon review of the SDK docs, there is a sample there on how to run processing in a separate thread. Has multi-threading become more welcome to Revit's API recently?

0 Likes