关闭通过事务获取的 AutoCAD 对象
Closing AutoCAD objects which were obtained via transaction
我正在编写 AutoCAD 插件。我正在使用交易获取一些对象,我想出了一个问题 - 我是否需要关闭(处置)我通过交易收到的对象?
文档中写道,当调用 Commit() 方法时,事务会关闭通过事务接收到的每个对象。
void Commit() -- This function commits the changes made in all the DBObjects opened during the Transaction, and then closes them.
但是当我不调用这个方法时会发生什么?例如,我仅使用事务来打开对象并接收其图层名称。类似于以下内容:
Database hostapp_workdb = HostApplicationServices.WorkingDatabase;
using ( Application.DocumentManager.MdiActiveDocument.LockDocument() )
using ( Transaction transaction = hostapp_workdb.TransactionManager.StartTransaction() )
{
Entity entity = transaction.GetObject(objectId, OpenMode.ForRead) as Entity;
if ( entity != null )
layer = entity.Layer;
}
如您所见,这里我没有调用 Commit()。在这种情况下会发生什么?实体是否关闭(因为事务正在使用它必须被处置,所以我认为它必须关闭所有对象。但我没有在文档中找到任何确认,所以这只是我的假设)。
也许我需要这样做:
ObjectId objectId = new ObjectId();
string layer = string.Empty;
Database hostapp_workdb = HostApplicationServices.WorkingDatabase;
using ( Application.DocumentManager.MdiActiveDocument.LockDocument() )
using ( Transaction transaction = hostapp_workdb.TransactionManager.StartTransaction() )
{
using ( Entity entity = transaction.GetObject(objectId, OpenMode.ForRead) as Entity )
{
if ( entity != null )
layer = entity.Layer;
}
}
鼓励链接到官方来源。
谢谢。
我记得读过 Kean Walmsley 写的 post,他提到如果你不使用 Commit(),事务将始终默认使用 Abort()(我会查一下)。
如果在交易中使用实体,它们将被自动处置。
Kean 在他的博客上有一些很好的例子。你绝对应该检查一下。
你可以找到它们 here
如果必须处置实体,您还会在编译器输出中收到通知。
编辑:
Forgetting to commit a Transaction
[...] An uncommitted Transaction is Aborted when it is Disposed, so
every change you made to the Database in the Transaction is rolled
back [...]
它是由 Stephen Preston post在 adndevblog 上编辑的 here
我正在编写 AutoCAD 插件。我正在使用交易获取一些对象,我想出了一个问题 - 我是否需要关闭(处置)我通过交易收到的对象?
文档中写道,当调用 Commit() 方法时,事务会关闭通过事务接收到的每个对象。
void Commit() -- This function commits the changes made in all the DBObjects opened during the Transaction, and then closes them.
但是当我不调用这个方法时会发生什么?例如,我仅使用事务来打开对象并接收其图层名称。类似于以下内容:
Database hostapp_workdb = HostApplicationServices.WorkingDatabase;
using ( Application.DocumentManager.MdiActiveDocument.LockDocument() )
using ( Transaction transaction = hostapp_workdb.TransactionManager.StartTransaction() )
{
Entity entity = transaction.GetObject(objectId, OpenMode.ForRead) as Entity;
if ( entity != null )
layer = entity.Layer;
}
如您所见,这里我没有调用 Commit()。在这种情况下会发生什么?实体是否关闭(因为事务正在使用它必须被处置,所以我认为它必须关闭所有对象。但我没有在文档中找到任何确认,所以这只是我的假设)。
也许我需要这样做:
ObjectId objectId = new ObjectId();
string layer = string.Empty;
Database hostapp_workdb = HostApplicationServices.WorkingDatabase;
using ( Application.DocumentManager.MdiActiveDocument.LockDocument() )
using ( Transaction transaction = hostapp_workdb.TransactionManager.StartTransaction() )
{
using ( Entity entity = transaction.GetObject(objectId, OpenMode.ForRead) as Entity )
{
if ( entity != null )
layer = entity.Layer;
}
}
鼓励链接到官方来源。
谢谢。
我记得读过 Kean Walmsley 写的 post,他提到如果你不使用 Commit(),事务将始终默认使用 Abort()(我会查一下)。
如果在交易中使用实体,它们将被自动处置。 Kean 在他的博客上有一些很好的例子。你绝对应该检查一下。 你可以找到它们 here
如果必须处置实体,您还会在编译器输出中收到通知。
编辑:
Forgetting to commit a Transaction
[...] An uncommitted Transaction is Aborted when it is Disposed, so every change you made to the Database in the Transaction is rolled back [...]
它是由 Stephen Preston post在 adndevblog 上编辑的 here