Visual Basic Customization

Visual Basic Customization

Reply
Contributor
dj-nemo
Posts: 16
Registered: ‎08-17-2012
Message 1 of 3 (332 Views)
Accepted Solution

writing in command line in ACD

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

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

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

 

*Expert Elite*
norman.yuan
Posts: 1,051
Registered: ‎04-27-2009
Message 2 of 3 (324 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...

 

Contributor
dj-nemo
Posts: 16
Registered: ‎08-17-2012
Message 3 of 3 (317 Views)

Re: writing in command line in ACD

08-18-2012 02:54 AM 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,..

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!