- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've been having a problem with Event Handlers and Transactions lately. I have attributes in a block that I am attaching an event handler to their Modified event.
The problem occurs when I move the block, the event handlers for the attributes trigger. But since the block is open for write since its being moved, I was getting errors when trying to open the block using OwnerId on the attribute.
Using OpenCloseTransaction's seems to through an error. But if I use StartTransaction instead, there is no error.
I thought I had read that OpenClose transactions were prefered for event handlers, but I guess not in this case.
Any light that could be shed on Transactions and when to use some kind over others would be greatly appreciated.
Here is some testing code I used to figure this out. It uses ExtensionMethods that aren't provided, but they are just wrappers that do all the transaction starting and getting of the object behind the scenes.
[CommandMethod(nameof(TestMarkerEvents))]
public void TestMarkerEvents()
{
ObjectId markerId = Active.Editor.GetUser<BlockReference>("Select Marker:");
if(markerId == ObjectId.Null) return;
//open the block in a StartOpenCloseTransaction
markerId.Get<BlockReference>((marker, tr) =>
{
//open each att using the Transaction tr
marker.AttributeCollection.ForEach(att =>
{
att.Modified += AttOnModified;
}, tr);
});
}
private void AttOnModified(object sender, EventArgs e)
{
AttributeReference att = sender as AttributeReference;
//works when using StartTransaction, does not work with StartOpenCloseTransaction
//does not work with tr = new OpenCloseTransaction
using (Transaction tr = Active.Database.TransactionManager.StartTransaction())
{
att.OwnerId.Get<BlockReference>(block =>
{
Active.WriteMessage($"\ntest");
}, tr);
}
}
Solved! Go to Solution.