EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 625|回复: 0
打印 上一主题 下一主题

[用户交互] 让AutoCAD.NET支持后台线程

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12404

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-8 14:20:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3. using System.Windows.Forms;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.Geometry;

  9. namespace BackgroundProcess
  10. {
  11.   public class Commands : IExtensionApplication
  12.   {
  13.     static Control syncCtrl;
  14.     public void Initialize()
  15.     {
  16.       // The control created to help with marshaling
  17.       // needs to be created on the main thread
  18.       syncCtrl = new Control();
  19.       syncCtrl.CreateControl();
  20.     }

  21.     public void Terminate()
  22.     {
  23.     }

  24.     void BackgroundProcess()
  25.     {
  26.       // This is to represent the background process
  27.       System.Threading.Thread.Sleep(5000);

  28.       // Now we need to marshall the call to the main thread
  29.       // I don't see how this could ever be false in this context,
  30.       // but I check it anyway
  31.       if (syncCtrl.InvokeRequired)
  32.         syncCtrl.Invoke(
  33.           new FinishedProcessingDelegate(FinishedProcessing));
  34.       else
  35.         FinishedProcessing();
  36.     }

  37.     delegate void FinishedProcessingDelegate();
  38.     void FinishedProcessing()
  39.     {
  40.       // If we want to modify the database, then we need to lock
  41.       // the document since we are in session/application context
  42.       Document doc = acApp.DocumentManager.MdiActiveDocument;
  43.       using (doc.LockDocument())
  44.       {
  45.         using (Transaction tr =
  46.           doc.Database.TransactionManager.StartTransaction())
  47.         {
  48.           BlockTable bt =
  49.             (BlockTable)tr.GetObject(
  50.             doc.Database.BlockTableId, OpenMode.ForRead);

  51.           BlockTableRecord ms = (BlockTableRecord)tr.GetObject(
  52.             bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

  53.           Line line =
  54.             new Line(new Point3d(0, 0, 0), new Point3d(10, 10, 0));
  55.           ms.AppendEntity(line);
  56.           tr.AddNewlyCreatedDBObject(line, true);

  57.           tr.Commit();
  58.         }
  59.       }

  60.       // Also write a message to the command line
  61.       // Note: using AutoCAD notification bubbles would be
  62.       // a nicer solution
  63.       // TrayItem/TrayItemBubbleWindow
  64.       Editor ed = acApp.DocumentManager.MdiActiveDocument.Editor;
  65.       ed.WriteMessage("Finished the background process!\n");
  66.     }

  67.     [CommandMethod("rocessInBackground")]
  68.     public void ProcessBackground()
  69.     {
  70.       // Let's say we got some data from the drawing and
  71.       // now we want to process it in a background thread
  72.       System.Threading.Thread thread =
  73.         new System.Threading.Thread(
  74.           new System.Threading.ThreadStart(BackgroundProcess));
  75.       thread.Start();

  76.       Editor ed = acApp.DocumentManager.MdiActiveDocument.Editor;
  77.       ed.WriteMessage(
  78.         "Started background processing. " +
  79.         "You can keep working as usual.\n");
  80.     }
  81.   }
  82. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对
工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-11-16 12:28

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

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