VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

writing in command line in ACD

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
dj-nemo
740 Views, 2 Replies

writing in command line in ACD

Hi

 

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("127.0.0.1") 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
2 REPLIES 2
Message 2 of 3
norman.yuan
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...

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3
dj-nemo
in reply to: norman.yuan

Hi

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,..

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

”Boost