Allow AutoCAD. Net to support Background threads

Source: Internet
Author: User
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.

UsingSystem;

UsingAutodesk. AutoCAD. runtime;

UsingSystem. Windows. forms;

UsingAutodesk. AutoCAD. editorinput;

Using Acapp= Autodesk. AutoCAD. applicationservices.Application;

UsingAutodesk. AutoCAD. applicationservices;

UsingAutodesk. AutoCAD. databaseservices;

UsingAutodesk. AutoCAD. geometry;

 

NamespaceBackgroundprocess

{

Public Class Commands:Iextensionapplication

{

Static ControlSyncctrl;

Public VoidInitialize ()

{

// The control created to help with creating aling

// Needs to be created on the main thread

Syncctrl =New Control();

Syncctrl. createcontrol ();

}

 

Public VoidTerminate ()

{

}

 

VoidBackgroundprocess ()

{

// 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 cocould ever be false in this context,

// But I check it anyway

If(Syncctrl. invokerequired)

Syncctrl. Invoke (

New Finishedprocessingdelegate(Finishedprocessing ));

Else

Finishedprocessing ();

}

 

Delegate Void Finishedprocessingdelegate();

VoidFinishedprocessing ()

{

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

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

DocumentDoc =Acapp. Documentmanager. mdiactivedocument;

Using(Doc. lockdocument ())

{

Using(TransactionTr =

Doc. database. transactionmanager. starttransaction ())

{

BlocktableBt =

(Blocktable) Tr. GetObject (

Doc. database. blocktableid,Openmode. Forread );

 

BlocktablerecordMS = (Blocktablerecord) Tr. GetObject (

BT [Blocktablerecord. Modelspace],Openmode. Forwrite );

 

LineLine =

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 wocould be

// A nicer Solution

// Trayitem/trayitembubblewindow

EditorEd =Acapp. Documentmanager. mdiactivedocument. Editor;

Ed. writemessage ("Finished the background process! \ N");

}

 

[Commandmethod("Processinbackground")]

Public VoidProcessbackground ()

{

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

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

System. threading.ThreadThread =

NewSystem. threading.Thread(

NewSystem. threading.Threadstart(Backgroundprocess ));

Thread. Start ();

 

EditorEd =Acapp. Documentmanager. mdiactivedocument. Editor;

Ed. writemessage (

"Started background processing ."+

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

}

}

}

 

original link: http://adndevblog.typepad.com/autocad/2012/06/use-thread-for-background-processing.html? Cid = 6a0167607c2431970b017742be1ec2970d

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.