carrot1983 发表于 2015-9-21 18:51

eLockViolation这个错误怎么解决?

本帖最后由 carrot1983 于 2015-9-21 18:53 编辑

试过很多次,也不知道什么情况。
测试环境:
VS2008
CAD2008
.NET3.5


using (Transaction tr = db.TransactionManager.StartTransaction())
                  {
                        DBText ent = tr.GetObject(entId, OpenMode.ForRead) as DBText;

//在块表记录这句出现了 eLockViolation ,没搞清楚为什么。。。请指点一二。
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);


                        Point3d center = new Point3d((pt1.X + pt2.X) * 0.5, (pt1.Y + pt2.Y) * 0.5, 0.0);
                        double radius = (pt1.DistanceTo(pt2)) * 0.6;
                        Circle circle = new Circle(center, Vector3d.ZAxis, radius);
                        btr.AppendEntity(circle);

                        tr.AddNewlyCreatedDBObject(circle, true);
                        tr.Commit();



carrot1983 发表于 2015-9-21 19:57

本帖最后由 carrot1983 于 2015-9-21 20:13 编辑

难道是因为我这个是用在 非模态对话框之中,才会有这种错误?
那怎么解决呢?

http://blog.csdn.net/habit2/article/details/2030476


最终还是得靠自己的不断折腾:

                  //锁住文档
                  Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                  using (doc.LockDocument())
                  using (Transaction tr = db.TransactionManager.StartTransaction())
                  {



搞定

carrot1983 发表于 2015-9-21 19:00

                  using (Transaction tr = db.TransactionManager.StartTransaction())
                  {
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForWrite);

改成这样子,也是出错。摸不着头脑了。

cooolseee 发表于 2015-9-24 09:47

你好,你用了using (doc.LockDocument())后,用不用DOC.DISPOS()

carrot1983 发表于 2015-9-24 14:00

cooolseee 发表于 2015-9-24 09:47 static/image/common/back.gif
你好,你用了using (doc.LockDocument())后,用不用DOC.DISPOS()

这么表示,好像是自动销毁,

cooolseee 发表于 2015-9-24 21:42

我看有些教程说锁文档的方法,在处理完成时,调用文档锁的Dispose()方法即可解锁

cdinten 发表于 2015-9-28 08:52

本帖最后由 cdinten 于 2015-9-28 09:02 编辑

cooolseee 发表于 2015-9-24 21:42 static/image/common/back.gif
我看有些教程说锁文档的方法,在处理完成时,调用文档锁的Dispose()方法即可解锁你回答的没错,但是在C#的语法中,using有三种用法:
1. using指令,使用命名空间,就像一个程序开头的using System;这样的
2. using别名,使用命名空间还可以起一个名字,例如using aClass = NameSpace1.MyToolsSets.MyClass;
3. using语句块,定义一个范围,在范围结束时自动处理对象。例如:
using (Class1 cls1 = new Class1(), cls2 = new Class1())
{
// cls1,cls2 can be used here.
} now cls1 and cls2 has been dispose automaticly.
//here you should never use cls1,cls2
但是需要明确的是,这种语法形势下, using括号中的对象,必须是实现了IDisposable接口的对象
所以,你的问题有两种解决方法:
1. 使用括号形式,后面就不再显式调用Dispose方法,编译器已经为你处理是此项工作。微软使用using语句块,就是方便开发人员在写了很长一段代码之后忘记释放资源。
2. 不使用using 语句,改用:
DocumentLock dl=doc.LockDocument();
//code goes here
dl.Dispose();
这样应该说清楚了吧?

cdinten 发表于 2015-9-28 08:57

另外,如果想详细了解using语句块的运行机理,可以写一个简单的程序然后查看其IL代码,这个最能说明问题。

cooolseee 发表于 2015-9-28 11:28

谢谢了,你说的using的三种用法这次我完整的学习了一下

幽静的瓦尔登湖 发表于 2022-11-1 09:21

carrot1983 发表于 2015-9-21 19:57
难道是因为我这个是用在 非模态对话框之中,才会有这种错误?
那怎么解决呢?



谢谢楼主,问题解决了~~~谢谢~!!
页: [1]
查看完整版本: eLockViolation这个错误怎么解决?