找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1000|回复: 0

[分享] 让AutoCAD.NET支持后台线程

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-1 15:52:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

×
http://adndevblog.typepad.com/autocad/2012/06/use-thread-for-background-processing.html?cid=6a0167607c2431970b017742be1ec2970d Use Thread for background processingBy 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");
    }
  }
}



论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 22:46 , Processed in 0.179705 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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