Why is vb .net repeatedly writing the same line of text in .txt?

Why is vb .net repeatedly writing the same line of text in .txt?

rita.aguiar
Advocate Advocate
1,427 Views
5 Replies
Message 1 of 6

Why is vb .net repeatedly writing the same line of text in .txt?

rita.aguiar
Advocate
Advocate

Why is vb .net repeatedly writing the same line of text in .txt?

 

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices Public Class Class1 <CommandMethod("AddAppEvent")> Public Sub AddAppEvent() AddHandler Application.SystemVariableChanged, AddressOf appSysVarChanged End Sub Public Sub appSysVarChanged(ByVal senderObj As Object, ByVal sysVarChEvtArgs As Autodesk.AutoCAD.ApplicationServices. SystemVariableChangedEventArgs) Dim oVal As Object = Application.GetSystemVariable(sysVarChEvtArgs.Name) Dim file As System.IO.StreamWriter file = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\rita.aguiar\Documents\AutoCAD plug-in\Registo de Eventos.txt", True)
file.WriteLine("O utilizador " & Environment.UserName & " encerrou o AutoCAD às " & DateTime.Now.ToString("hh:mm, dddd, dd MMMM yyyy"))
file.Close() End Sub End Class

 

Example Output in .txt file:

Here it wrote 4 times, sometimes it writes more, others less. Why is this random thing happening? Did anyone have the same problem? Is this some kind of bug? I just want it to write once.

O utilizador rita.aguiar encerrou o AutoCAD às 01:11, segunda-feira, 10 setembro 2018

O utilizador rita.aguiar encerrou o AutoCAD às 01:11, segunda-feira, 10 setembro 2018

O utilizador rita.aguiar encerrou o AutoCAD às 01:11, segunda-feira, 10 setembro 2018

O utilizador rita.aguiar encerrou o AutoCAD às 01:11, segunda-feira, 10 setembro 2018

Many thanks.

 

0 Likes
Accepted solutions (1)
1,428 Views
5 Replies
Replies (5)
Message 2 of 6

rita.aguiar
Advocate
Advocate

I've realized it has to do with the system variable:
Manly these are happening

VIEWBACKSTATUS O

RIBBONSTATE O

 

What are these?

0 Likes
Message 3 of 6

norman.yuan
Mentor
Mentor

There is nothing wrong with AutoCAD responding to your code: whenever ANYONE of AutoCAD's system variables changes, your code would be executed (after your command "AddAppEvent" being called). During an AutoCAD session, depending on what AutoCAD does, many system variables changes. Since your code in the SystemVariableChanged even handler does the file writing no matter what system variable changes, you would get a lot of more lines of text written to the text file if you keep AutoCAD running long time.

 

What is user purpose of writing text file in the event handler? If you want to keep track of which user is using AutoCAD (just my guess), you DO NOT need to handle systemvariablechanged event, because only ONE user runs AutoCAD in entire AutoCAD session. You simply need to write 1 line of text per AutoCAD session, say, on its startup, or when AutoCAD is quit (well, AutoCAD may crash, so if you do it on its quit, you may miss the chance).

 

By the way, this forum is mainly meant for AutoCAD VBA/COM API, while your question is about AutoCAD.NET API. You might want to post question in .NET group for better help.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 6

rita.aguiar
Advocate
Advocate

Thank you for your answer.

I want to register when the user closes the AutoCAD application.
I just used the BeginQuit event that I found here:

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html

it says its for BeginQuit and it uses the

SystemVariableChanged
0 Likes
Message 5 of 6

norman.yuan
Mentor
Mentor
Accepted solution
If you only want to log who closes AutoCAD, then you only need to handle BeginQuit event, it has nothing to do with system variable changes (i.e. no mater what system variable changes what AutoCAD starts closing, the login user does not change. So, simply in you command: AddHandler Application.BeginQuit, AddressOf AcadBeginQuit Then in the event handler, Private Sub AcadBeginQuit(....) Dim file As System.IO.StreamWriter file = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\rita.aguiar\Documents\AutoCAD plug-in\Registo de Eventos.txt", True) file.WriteLine("O utilizador " & Environment.UserName & " encerrou o AutoCAD às " & DateTime.Now.ToStrin("hh:mm, dddd, dd MMMM yyyy")) file.Close() End Sub

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 6

rita.aguiar
Advocate
Advocate

Thank you.
I guess the autocad page I was reading wasn't even right.

 

0 Likes