让AutoCAD.NET支持后台线程

简介: Use Thread for background processing By Adam Nagy From inside my command I'm starting a background thread that synchronizes with a database.

Use Thread for background processing

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

目录
相关文章
|
1月前
|
数据处理
Swing通过后台线程实现页面更新
Swing通过后台线程实现页面更新
20 2
|
8月前
|
安全 Java Android开发
Android 中AsyncTask后台线程,异步任务的理解
Android 中AsyncTask后台线程,异步任务的理解
101 0
|
5月前
|
缓存 开发框架 前端开发
基于.NET 7 + iView 的前后端分离的通用后台管理系统开源框架
基于.NET 7 + iView 的前后端分离的通用后台管理系统开源框架
44 0
|
9月前
|
存储 开发框架 .NET
WPF+ASP.NET SignalR实现后台通知
WPF+ASP.NET SignalR实现后台通知
81 0
|
9月前
|
前端开发
解决.NET Core Ajax请求后台传送参数过大请求失败问题
解决.NET Core Ajax请求后台传送参数过大请求失败问题
|
SQL 开发框架 前端开发
Asp.net core项目实战 新闻网站+后台 源码、设计原理、视频教程
Asp.net core项目实战 新闻网站+后台 源码、设计原理、视频教程
306 0
Asp.net core项目实战 新闻网站+后台 源码、设计原理、视频教程
|
开发框架 前端开发 .NET
|
Java 开发者
后台守护线程|学习笔记
快速学习 后台守护线程
|
开发框架 .NET 数据安全/隐私保护
基于ASP.Net Core开发的一套通用后台框架
基于ASP.Net Core开发的一套通用后台框架写在前面这是本人在学习的过程中搭建学习的框架,如果对你有所帮助那再好不过。如果您有发现错误,请告知我,我会第一时间修改。知其然,知其所以然,并非重复的造轮子。
18446 0
|
.NET 数据库 安全
ASP.NET | 从零到一实战分析对后台数据库增加数据、模糊查找、精确查找并展示
新建: 新建数据库mydb.mdf、添加表: 更新数据库; 右击数据库,选择属性,复制连接字符串;(这里之前做过了,就简单点过不做细节。
1305 0