Visual Basic Customization

Visual Basic Customization

Posts: 16
Registered: ‎08-17-2012
Message 1 of 3 (320 Views)
Accepted Solution

writing in command line in ACD

320 Views, 2 Replies
08-17-2012 05:28 AM



I am working on tcp server plugin for ACD. I want that server write in commandline history what's happenning. 


When i run plugin followng error ocurs: System Null Reference Exception



That is my code:


Imports System.Net.Sockets
Imports System.Text
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports System.Net
Imports System.Threading
Imports Autodesk.AutoCAD.EditorInput



Public Class Commands Dim close As Boolean = True Dim tcpListener As TcpListener Dim tcpClient As TcpClient Dim mThread As Thread <CommandMethod("StartServer")> Public Sub StartServer() Const portNumber As Integer = 8000 Dim ip As IPAddress = IPAddress.Parse("") tcpListener = New TcpListener(ip, portNumber) mThread = New Thread(AddressOf MainThread) mThread.Start() End Sub Private Sub MainThread() tcpListener.Start() Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor ed.WriteMessage("Waiting for connection...") While (close) Try tcpClient = tcpListener.AcceptTcpClient() ed.WriteMessage("Connection accepted.") If (tcpClient.Connected) Then Dim thread As Thread = New Thread(AddressOf ThreadTask) thread.Start() End If Catch e As Exception End Try End While End Sub End Class


It shows that error happened here: 

 Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
*Expert Elite*
Posts: 987
Registered: ‎04-27-2009
Message 2 of 3 (312 Views)

Re: writing in command line in ACD

08-17-2012 07:42 AM in reply to: dj-nemo

Firstly, this forum is traditionally for AutoCAD VBA/COM API discussion, although the name says "Visual Basic Customization" (anyone started doing Window programming before .NET era regards VB/VBA and VB.NET a complete different thing. Your .NET API question might get better chance of discussion in .NET forum.


AutoCAD itself does not work in multi-thread. Yes, you can start a new thread in your .NET add-in, so long as the new thread does not deal with AutoCAD itself, such as service call, Tcp communication (as you did). In your case, you CANNOT use Acad object (Editor, in your case) in the new thread, hence the error.


Also, you have to be very careful how how/what do you do with AutoCAd when the side thread finished (you need to do something, or what is the point to run things in another thread in AutoCAD, right?). When the side thread doing something (geting some data, usually) completes, you cannot just go straight to call something in the main AutoCAD thread, because AutoCAD might be in the middle of something/ in a process of a command execution.


You might do this (I have not tried, though): creating a stack/queue data structure in main Acad thread to store the result of side thread process. Then handle Application.Idle/Editor/EnteringQuiencentState event to get data from the Stack/Queue...


Posts: 16
Registered: ‎08-17-2012
Message 3 of 3 (305 Views)

Re: writing in command line in ACD

08-18-2012 02:54 AM in reply to: norman.yuan


Thank you for your answer it helped me a lot and 

I am sorry for writing on wrong forum, i am totaly new in autocad, vb .net,..


Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.