明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2696|回复: 9

[图元] eLockViolation这个错误怎么解决?

[复制链接]
发表于 2015-9-21 18:51 | 显示全部楼层 |阅读模式
本帖最后由 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();



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 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())
                    {



搞定
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 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[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

改成这样子,也是出错。摸不着头脑了。
发表于 2015-9-24 09:47 | 显示全部楼层
你好,你用了using (doc.LockDocument())后,用不用DOC.DISPOS()
 楼主| 发表于 2015-9-24 14:00 | 显示全部楼层
cooolseee 发表于 2015-9-24 09:47
你好,你用了using (doc.LockDocument())后,用不用DOC.DISPOS()

这么表示,好像是自动销毁,
发表于 2015-9-24 21:42 | 显示全部楼层
我看有些教程说锁文档的方法,在处理完成时,调用文档锁的Dispose()方法即可解锁
发表于 2015-9-28 08:52 | 显示全部楼层
本帖最后由 cdinten 于 2015-9-28 09:02 编辑
cooolseee 发表于 2015-9-24 21:42
我看有些教程说锁文档的方法,在处理完成时,调用文档锁的Dispose()方法即可解锁
你回答的没错,但是在C#的语法中,using有三种用法:
1. using指令,使用命名空间,就像一个程序开头的using System;这样的
2. using别名,使用命名空间还可以起一个名字,例如using aClass = NameSpace1.MyToolsSets.MyClass;
3. using语句块,定义一个范围,在范围结束时自动处理对象。例如:
  1. using (Class1 cls1 = new Class1(), cls2 = new Class1())
  2. {
  3.   // cls1,cls2 can be used here.
  4. } now cls1 and cls2 has been dispose automaticly.
  5. //here you should never use cls1,cls2

但是需要明确的是,这种语法形势下, using括号中的对象,必须是实现了IDisposable接口的对象
所以,你的问题有两种解决方法:
1. 使用括号形式,后面就不再显式调用Dispose方法,编译器已经为你处理是此项工作。微软使用using语句块,就是方便开发人员在写了很长一段代码之后忘记释放资源。
2. 不使用using 语句,改用:
  1. DocumentLock dl=doc.LockDocument();
  2. //code goes here
  3. dl.Dispose();

这样应该说清楚了吧?
发表于 2015-9-28 08:57 | 显示全部楼层
另外,如果想详细了解using语句块的运行机理,可以写一个简单的程序然后查看其IL代码,这个最能说明问题。
发表于 2015-9-28 11:28 | 显示全部楼层
谢谢了,你说的using的三种用法这次我完整的学习了一下
发表于 2022-11-1 09:21 | 显示全部楼层
carrot1983 发表于 2015-9-21 19:57
难道是因为我这个是用在 非模态对话框之中,才会有这种错误?
那怎么解决呢?

谢谢楼主,问题解决了~~~谢谢~!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-29 22:15 , Processed in 0.245469 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表