让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

目录
相关文章
|
6月前
|
并行计算 安全 Java
C# .NET面试系列四:多线程
<h2>多线程 #### 1. 根据线程安全的相关知识,分析以下代码,当调用 test 方法时 i > 10 时是否会引起死锁? 并简要说明理由。 ```c# public void test(int i) { lock(this) { if (i > 10) { i--; test(i); } } } ``` 在给定的代码中,不会发生死锁。死锁通常是由于两个或多个线程互相等待对方释放锁而无法继续执行的情况。在这个代码中,只有一个线程持有锁,且没有其他线程参与,因此不
381 3
|
23天前
|
监控 网络安全 调度
Quartz.Net整合NetCore3.1,部署到IIS服务器上后台定时Job不被调度的解决方案
解决Quartz.NET在.NET Core 3.1应用中部署到IIS服务器上不被调度的问题,通常需要综合考虑应用配置、IIS设置、日志分析等多个方面。采用上述策略,结合细致的测试和监控,可以有效地提高定时任务的稳定性和可靠性。在实施任何更改后,务必进行充分的测试,以验证问题是否得到解决,并监控生产环境的表现,确保长期稳定性。
37 1
|
5月前
|
开发框架 监控 Java
【.NET Core】多线程之线程池(ThreadPool)详解(二)
【.NET Core】多线程之线程池(ThreadPool)详解(二)
88 3
|
5月前
|
SQL 开发框架 Java
【.NET Core】多线程之线程池(ThreadPool)详解(一)
【.NET Core】多线程之线程池(ThreadPool)详解(一)
374 2
|
5月前
|
算法 安全 Java
【.NET Core】 多线程之(Thread)详解
【.NET Core】 多线程之(Thread)详解
60 1
|
6月前
|
监控 网络协议 iOS开发
程序退到后台的时候,所有线程被挂起,系统回收所有的socket资源问题及解决方案
程序退到后台的时候,所有线程被挂起,系统回收所有的socket资源问题及解决方案
186 0
|
6月前
|
数据处理
Swing通过后台线程实现页面更新
Swing通过后台线程实现页面更新
74 2
|
安全 Java Android开发
Android 中AsyncTask后台线程,异步任务的理解
Android 中AsyncTask后台线程,异步任务的理解
154 0
|
缓存 开发框架 前端开发
基于.NET 7 + iView 的前后端分离的通用后台管理系统开源框架
基于.NET 7 + iView 的前后端分离的通用后台管理系统开源框架
77 0
|
监控 Java 数据库连接
【JavaSE专栏86】守护线程的那些事,后台默默地守护,是最长情的告白
【JavaSE专栏86】守护线程的那些事,后台默默地守护,是最长情的告白