惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
Cisco Talos Blog
Cisco Talos Blog
MongoDB | Blog
MongoDB | Blog
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 叶小钗
Help Net Security
Help Net Security
美团技术团队
Attack and Defense Labs
Attack and Defense Labs
雷峰网
雷峰网
S
Security @ Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
AI
AI
Hacker News: Ask HN
Hacker News: Ask HN
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
Last Week in AI
Last Week in AI
S
Secure Thoughts
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
A
Arctic Wolf
Y
Y Combinator Blog
MyScale Blog
MyScale Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
罗磊的独立博客
Vercel News
Vercel News
D
DataBreaches.Net
博客园 - 聂微东
P
Palo Alto Networks Blog
N
News | PayPal Newsroom
GbyAI
GbyAI
B
Blog
A
About on SuperTechFans
PCI Perspectives
PCI Perspectives
S
Schneier on Security
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
The GitHub Blog
The GitHub Blog
P
Privacy International News Feed
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News

博客园 - pensir

ef报错:实体类型XXX不是当前上下文的模型的一部分。 将cad嵌入到vb中 sql server 2005 msxml安装失败解决及msxml 6.0 卸载 智能替换DataTable.Select中会导致错误的单引号 - pensir - 博客园 CADVBA代码移植到.NET 最全ASCII码对照表(备用) C# 删除字符串中任何位置的空格 - pensir - 博客园 在VB.NET中使用MS Access存储过程(转载) Access存储过程,环境:VB 2005+.NET2.0+ACCESS2003(转载) 关键性代码整理 WinForm窗体间传值 WinForm主要控件分类一览 MaskedTextBox掩码元素一览 ArcGIS中的坐标系统 Geodatabase组织结构 GIS关键词 栅格数据与矢量数据 ArcGIS的文件结构 winform+c#之窗体之间的传值 - pensir - 博客园
让AutoCAD.NET支持后台线程
pensir · 2012-10-20 · via 博客园 - pensir

By Adam Nagy

From inside my command I'm starting a background thread that synchronizes with a database. Once that finishes I'd like to keep using the AutoCAD .NET API to do some further modifications in the database. Unfortunately, when I'm calling the AutoCAD API functions from this thread things do not seem to work, e.g. the MdiActiveDocument is null.

Solution

The AutoCAD API's do not support multithreading. You should only call the API functions from the main thread.

If you are in a different thread then you have to marshal the call to the main thread. The simplest way to achieve that is creating a System.Windows.Forms.Control object on the main thread and call its Invoke() function to start the function that is doing the end processing.

Here is a sample to demonstrate this.

using System;

using Autodesk.AutoCAD.Runtime;

using System.Windows.Forms;

using Autodesk.AutoCAD.EditorInput;

using acApp = Autodesk.AutoCAD.ApplicationServices.Application;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

namespace BackgroundProcess

{

  public class Commands : IExtensionApplication

  {

    static Control syncCtrl;

    public void Initialize()

    {

      // The control created to help with marshaling

      // needs to be created on the main thread

      syncCtrl = new Control();

      syncCtrl.CreateControl();

    }

    public void Terminate()

    {

    }

    void BackgroundProcess()

    {

      // This is to represent the background process

      System.Threading.Thread.Sleep(5000);

      // Now we need to marshall the call to the main thread

      // I don't see how this could ever be false in this context,

      // but I check it anyway

      if (syncCtrl.InvokeRequired)

        syncCtrl.Invoke(

          new FinishedProcessingDelegate(FinishedProcessing));

      else

        FinishedProcessing();

    }

    delegate void FinishedProcessingDelegate();

    void FinishedProcessing()

    {

      // If we want to modify the database, then we need to lock

      // the document since we are in session/application context

      Document doc = acApp.DocumentManager.MdiActiveDocument;

      using (doc.LockDocument())

      {

        using (Transaction tr =

          doc.Database.TransactionManager.StartTransaction())

        {

          BlockTable bt =

            (BlockTable)tr.GetObject(

            doc.Database.BlockTableId, OpenMode.ForRead);

          BlockTableRecord ms = (BlockTableRecord)tr.GetObject(

            bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

          Line line =

            new Line(new Point3d(0, 0, 0), new Point3d(10, 10, 0));

          ms.AppendEntity(line);

          tr.AddNewlyCreatedDBObject(line, true);

          tr.Commit();

        }

      }

      // Also write a message to the command line

      // Note: using AutoCAD notification bubbles would be

      // a nicer solution :)

      // TrayItem/TrayItemBubbleWindow

      Editor ed = acApp.DocumentManager.MdiActiveDocument.Editor;

      ed.WriteMessage("Finished the background process!\n");

    }

    [CommandMethod("ProcessInBackground")]

    public void ProcessBackground()

    {

      // Let's say we got some data from the drawing and

      // now we want to process it in a background thread

      System.Threading.Thread thread =

        new System.Threading.Thread(

          new System.Threading.ThreadStart(BackgroundProcess));

      thread.Start();

      Editor ed = acApp.DocumentManager.MdiActiveDocument.Editor;

      ed.WriteMessage(

        "Started background processing. " + 

        "You can keep working as usual.\n");

    }

  }

}

原文链接:http://adndevblog.typepad.com/autocad/2012/06/use-thread-for-background-processing.html?cid=6a0167607c2431970b017742be1ec2970d