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:
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.Edit
or 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.Edit
Solved! 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...
Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register
Start with some of our most frequented solutions to get help installing your software.