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
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
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
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.