Closed grizoood closed 6 months ago
try to dispose the transaction manually rather than relying on the "using" statement.
Or you can create a transaction in every loop
On the second pass through the loop I get this exception :
await RevitTask.RunAsync(
async app =>
{
try
{
var document = app.ActiveUIDocument.Document;
int count = 20;
for (int i = 0; i < count; i++)
{
string family_2d = "Family";
string symbol_2d = "Symbol";
using (Transaction transaction = new Transaction(document))
{
transaction.Start("Insert");
var symbol = document.FindSymbol(family_2d, symbol_2d);
FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
await Task.Delay(1000);
transaction.Commit();
}
Informations = $"Insert {i + 1}/{count}";
}
Informations = "Finished";
}
catch (Exception ex)
{ }
});
How about delaying after the transaction has been committed?
I tried too but it causes the same exception, for your information it happens on this line: " transaction.Start("Insert");"
What is the exception?
The 1st transaction is commited but the next one does not work.
The exception is: "Starting a transaction from an external application running outside of API context is not allowed." and this happens at the line: " transaction.Start("Insert");"
await RevitTask.RunAsync(
async app =>
{
try
{
var document = app.ActiveUIDocument.Document;
int count = 20;
for (int i = 0; i < count; i++)
{
string family_2d = "Family";
string symbol_2d = "Symbol";
using (Transaction transaction = new Transaction(document))
{
transaction.Start("Insert");
var symbol = document.FindSymbol(family_2d, symbol_2d);
FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
transaction.Commit();
}
await Task.Delay(1000);
Informations = $"Insert {i + 1}/{count}";
}
Informations = "Finished";
}
catch (Exception ex)
{ }
});
Exactly. The Task.Delay() results in the rest code to be running in a worker thread. You can try to use RevitTask.RunAsync in the loop to execute Revit API
It works by doing this:
try
{
int count = 20;
for (int i = 0; i < count; i++)
{
string family_2d = "Family";
string symbol_2d = "Symbol";
await RevitTask.RunAsync(
app =>
{
Informations = $"Insert {i + 1}/{count}";
var document = app.ActiveUIDocument.Document;
using (Transaction transaction = new Transaction(document))
{
transaction.Start("Insert");
var symbol = document.FindSymbol(family_2d, symbol_2d);
FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
transaction.Commit();
}
});
await Task.Delay(1000);
}
Informations = "Finished";
}
catch (Exception ex)
{
}
I was thinking of creating a single transaction for the loop how can I do that? Will creating multiple transactions impact processing time?
Another question: If I want to update the "Information" property of my viewmodel, in RevitTask.RunAsync, I have to use the Dispatcher of my interface and do this:
public void UpdateInformations(string message)
{
_dispatcher.Invoke(() =>
{
Informations = message;
}, DispatcherPriority.Background);
}
As far as I know, you can update viewmodel properties without caring about which thread your code is currently running. The viewmodel fires the "PropertyChanged" event to notify the UI to update in proper time.
try
{
RevitTask.RunAsync(async (app) =>
{
int count = 20;
var document = app.ActiveUIDocument.Document;
Transaction transaction = new Transaction(document);
transaction.Start("Insert");
var symbol = document.FindSymbol(family_2d, symbol_2d);
for (int i = 0; i < count; i++)
{
string family_2d = "Family";
string symbol_2d = "Symbol";
await RevitTask.RunAsync(
app =>
{
Informations = $"Insert {i + 1}/{count}";
FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
});
await Task.Delay(1000);
}
transaction.Commit();
transaction.Dispose();
Informations = "Finished";
});
}
catch (Exception ex)
{
}
How about this?
I get this exception: "System.Runtime.InteropServices.SEHException : 'Un composant externe a levé une exception.'"
How did you implement your viewmodel? Especially in the "Information" property
With your example I get this exception at this line:
FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
try
{
RevitTask.RunAsync(async (app) =>
{
int count = 20;
var document = app.ActiveUIDocument.Document;
Transaction transaction = new Transaction(document);
transaction.Start("Insert");
for (int i = 0; i < count; i++)
{
string family_2d = "Family";
string symbol_2d = "Symbol";
await RevitTask.RunAsync(
app =>
{
Informations = $"Insert {i + 1}/{count}";
var document = app.ActiveUIDocument.Document;
var symbol = document.FindSymbol(family_2d, symbol_2d);
FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
});
await Task.Delay(1000);
}
transaction.Commit();
transaction.Dispose();
Informations = "Finished";
});
}
catch (Exception ex)
{
}
Sorry, I don't have access to Revit at the moment. Please try entering the code again.
It doesn't change anything, same exception
try
{
RevitTask.RunAsync(async (app) =>
{
int count = 20;
var document = app.ActiveUIDocument.Document;
Transaction transaction = new Transaction(document);
transaction.Start("Insert");
for (int i = 0; i < count; i++)
{
string family_2d = "Family";
string symbol_2d = "Symbol";
await RevitTask.RunAsync(
app2 =>
{
Informations = $"Insert {i + 1}/{count}";
var document2 = app2.ActiveUIDocument.Document;
var symbol = document2.FindSymbol(family_2d, symbol_2d);
FamilyInstance instance = document2.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
});
await Task.Delay(1000);
}
transaction.Commit();
transaction.Dispose();
Informations = "Finished";
});
}
catch (Exception ex)
{
}
I haven't used Revit in years. It seems that the beginning and committing of a transaction cannot be separated in different Revit contexts.
After this is only a part of code in fact I encounter another problem, when I execute my command my Information property is not updated in my interface, I only see the last message of the loop
try
{
int count = 20;
for (int i = 0; i < count; i++)
{
string family_2d = "Family";
string symbol_2d = "Symbol";
// update information right before you enter an external event handler
Informations = $"Insert {i + 1}/{count}";
await RevitTask.RunAsync(
app =>
{
var document = app.ActiveUIDocument.Document;
using (Transaction transaction = new Transaction(document))
{
transaction.Start("Insert");
var symbol = document.FindSymbol(family_2d, symbol_2d);
FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
transaction.Commit();
}
});
await Task.Delay(1000);
}
Informations = "Finished";
}
catch (Exception ex)
{
}
Slight changes to the working code.
Is the issue still ongoing?
Yes but I close the issue
Hello
I'm trying to add a 1sec delay between each instance insertion but I'm having a problem, my transaction is rollback.