Multi-threaded through the API

Multi-threaded through the API

filat
Advisor Advisor
2,007 Views
19 Replies
Message 1 of 20

Multi-threaded through the API

filat
Advisor
Advisor

You can organize a multi-threaded work with data through Inventor API? For example, there is a assembly and I need the first part of assembling a list of items to consider in a data set, the second part of the list - in the second data set, the third - the third and so on up to N, where N - depends on how many threads can run in the system . Whether these flows are processed and to receive data in parallel? Or go to the consistent implementation of this algorithm will first be processed by the first part, and only after it will start the second ...?

0 Likes
2,008 Views
19 Replies
Replies (19)
Message 2 of 20

philippe.leefsma
Alumni
Alumni

Hi,

 

Basically the Inventor API is not multi threaded, so there is little advantage to write a multi-threaded application if several threads are going to perform API calls. What will happen is that each call will be queued by the COM server and processed sequentially. You can however have a multi-theaded application where a single thread access the Inventor API and other threads perform custom logic, either by using the data stored by the Inventor thread, access your custom database and so on...

 

I hope it helps.

 

Regards,

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 20

Anonymous
Not applicable

Has the multi-threaded status of the Inventor API changed?

0 Likes
Message 4 of 20

philippe.leefsma
Alumni
Alumni

Hi Eric,

 

No and there is no plan to provide multi-threaded capabilities to the API in a near future unfortunately. I would say, for the reason I provided above, as long as the API is a COM API, you have no chance to see it multi-threaded.

 

Regards,

Philippe. 



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 20

amitabhVA4SD
Advocate
Advocate

Opening up this old thread for discussion again.

@philippe.leefsma- What will happen if we query geometry (brep) information from multiple parts in a Parallel.ForEach loop in the same inventor session? Will Inventor Crash or will the COM server make the parallel task sequential?

 

Thanks,

Amitabh Mukherjee

0 Likes
Message 6 of 20

Maxim-CADman77
Advisor
Advisor

Dear @philippe.leefsma and @MjDeck

1. Is there support for multi-thread API calls in Inventor 2026?

2. If it possible to use more then one threads where only one would use Inventor API in iLogic rule?

I've tried to run the simplest two-thread code (none uses Inventor API) but got Inventor hanged (expected rule duration less than 10 sec):

Sub Main()
	' Create an Auxiliary thread
	Dim myThread As New Threading.Thread(AddressOf DoAuxWork)
	myThread.Start()

	' Main thread
	For i = 0 To 7 * iterationQty
		Logger.Info("i: " & i)
		Threading.Thread.Sleep(55)
	Next
End Sub

Dim iterationQty As Integer = 2

Sub DoAuxWork()
	For j = 0 To 5 * iterationQty
		Logger.Info(vbTab & "j: " & j)
		Threading.Thread.Sleep(77)
	Next
End Sub

What I'm missing?

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 7 of 20

hollypapp65
Advocate
Advocate

Don't think you can start new thread in iLogic or VBA inside IV.

Might be to do that with standalone.  Starting multiple IV sessions in different threads.

Also need to manage and check if threads finished their task or hang and close them.

Need to be careful with file access also.  You'll have no idea when a thread need to lock the file when it write to it.

Crash, hang and memory leak are common with bad multithreading.

 

What are you trying to do that need multithread?

0 Likes
Message 8 of 20

jjstr8
Collaborator
Collaborator

Logger.Info is an iLogic API call. Change your DoAuxWork (line 20 below) to write to debug listeners, not the iLogic logger.

 

jjstr8_0-1746629000939.png

jjstr8_1-1746629018744.png

 

 

Message 9 of 20

MjDeck
Autodesk
Autodesk

Hi @Maxim-CADman77 - as @jjstr8 said, the problem is with the call to Logger.Info. It is not designed to be called from a different thread. I'll look into that, but maybe the best we can do is throw an error instead of hanging.

Here's a version of your rule that will write the output of both threads to a single log file. You can view this file as it is being updated by using Notepad++ in monitoring mode. That's on the View menu (at or near the bottom of the list) : Monitoring (tail -f)

Option Explicit On
Imports Path = System.IO.Path
Imports File = System.IO.File
Imports System.IO
Sub Main()

Dim logFolder = Path.GetDirectoryName(logPath)
Directory.CreateDirectory(logFolder)

' Create an Auxiliary thread
Dim myThread As New Threading.Thread(AddressOf DoAuxWork)
myThread.Start()

' Main thread
For i = 0 To 7 * iterationQty
	AppendLineToLogFile("i: " & i)
	'Logger.Info("i: " & i)
	Threading.Thread.Sleep(55)
Next
End Sub

Dim iterationQty As Integer = 2

Dim logPath As String = "C:\Temp\InventorMultithread1.log"
Private ReadOnly _logFileLockObject As New Object()

Sub DoAuxWork()
	For j = 0 To 5 * iterationQty
		AppendLineToLogFile(vbTab & "j: " & j)
		'Logger.Info(vbTab & "j: " & j)
		Threading.Thread.Sleep(77)
	Next
End Sub

Sub AppendLineToLogFile(line As String)
	SyncLock _logFileLockObject
		AppendLineToFile(logPath, line)
	End SyncLock
End Sub

Sub AppendLineToFile(filePath As String, line As String)
	Using writer As New StreamWriter(filePath, True) ' open for append
		writer.WriteLine(line)
	End Using
End Sub

 


Mike Deck
Software Developer
Autodesk, Inc.

Message 10 of 20

jjstr8
Collaborator
Collaborator

@Maxim-CADman77 : What are you ultimately trying to accomplish? It's more difficult to suggest solutions without the full picture. 

0 Likes
Message 11 of 20

Maxim-CADman77
Advisor
Advisor

Now I'm just playing with the Multi-Threading to understand what I can do using it.

Let say, I want to periodically request (actualize) some data from some outside system (to make the sample real - let it request global DateTime from time.windows.com NTP-server once per 10 minutes).

 

I managed to do it in auxiliary thread but it seems to occasionally hang if left to work for the whole night.

 

What are my options?

I believe I need to somehow track the task state to determine when it hangs, then kill it task and start another instance of the task, right?

Where should I start?
When I pause the debug of the project in Visual Studio I see there are about 50-70 threads related to Inventor.

I see one that is definitely related to my Add-In (<project name>StandardAddInServer..) and it does have "v" control to expand and here what is inside:

MaximCADman77_0-1747653335930.png

What next?

How do I debug the auxiliary thread?

 

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 12 of 20

jjstr8
Collaborator
Collaborator

Here's an article with some tips.  How Do I Debug Async Code in Visual Studio?  Some of the tips may be a little overkill for the situation you're describing, but it's worth a read. There's also plenty of other async-await articles out there.

 

Without seeing your code, it's hard to guess at what's happening. I can't imagine any scenario where you would want to "handle" a deadlocked/hung task by default. There should always be some mechanism to allow the task to complete, be aborted/cancelled, or timeout. In the context of an Inventor addin, giving the user the appearance that all is well by way of the occasional UI updates is probably best. In some cases it's just not practical to break up your code for the sake of firing off async chunks. You'll see Inventor do it both ways; the progress bar at the bottom when opening a large assembly versus just the swirly wait cursor for some commands have an enormous amount of processing to do. In your example, I would try your code as a separate application and make sure it works independent of Inventor. Build a simple UI that reports on what your async code returns when it finishes, and keep it looping if that's what it needs to do.

 

0 Likes
Message 13 of 20

Maxim-CADman77
Advisor
Advisor

I really don't understand what might be wrong with that simple workflow that I'm testing.
It uses auxiliary thread to request the Global DateTime from ntp-server, writes the value to the 'globDateTime' variable and add to the log file (Notepad Plus Plus is supposed to be installed in "C:\Program Files\Notepad++\" to monitor the log), and it does nothing in main thread (even not use the 'globDateTime' variable).

 

You can find the code of this project here - https://github.com/CadMan77/AddinWithAuxThread.

 

Today this AddIn being left intact in Inventor 2024.5.3 (in Visual Studio debug mode) worked for about 7 hours and then just stopped to write the value to the log without giving any error or exception.

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 14 of 20

jjstr8
Collaborator
Collaborator

I looked at your code. It's likely an issue with getting the time. Socket.Receive is blocking by default, so it's probably just sitting there after something got lost on the time server end. You can either add a Socket.ReceiveTimeout or set Socket.Blocking to false. I recommend setting the timeout, but either way you'll need to put it in a Try..Catch to handle the SocketException. The other way to troubleshoot is to supply the test data yourself in the Task.Run code. Just write DateTime.Now to log file and see if it ever gets hung up.

 

Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim ntpData As Byte() = New Byte(47) {}
Try
    s.ReceiveTimeout = 5000
    s.Connect(ep)
    ntpData(0) = &H1B ' NTP request header
    s.Send(ntpData)
    s.Receive(ntpData)
    s.Close()
Catch
    Return DateTime.Now 'or whatever
End Try

 

EDIT: I just noticed you had a Try..Catch up the call stack. Just hand off the exception to that code.

Try
...
Catch
    Throw
End Try
0 Likes
Message 15 of 20

Maxim-CADman77
Advisor
Advisor

Adding Throw didn't help

MaximCADman77_0-1747807435047.png

 

Now it stopped write DateTime to log after 4.5 hours (again without error or exception).

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 16 of 20

jjstr8
Collaborator
Collaborator

@Maxim-CADman77 : Sorry I didn't clarify, but the Throw statement was for change to GetNetworkTime that I was showing. It doesn't impact your issue though. Have you run your code without running Notepad++? There were numerous issues reported with its monitoring mode.

0 Likes
Message 17 of 20

maxim.teleguz
Advocate
Advocate

you can start inventor with the command flag -Embedding

this will allow you to use those separate inventor processes for threading calculations. 

0 Likes
Message 18 of 20

Maxim-CADman77
Advisor
Advisor

@jjstr8 
Just got same issue reproduced without running Notepad++

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 19 of 20

jjstr8
Collaborator
Collaborator

@Maxim-CADman77 : I left mine running yesterday for about 9 hours against our local network NTP server (via VPN) with no issues, then put my laptop to sleep. I came in today and it fired back up. I only had two exceptions thrown from GetNetworkTime today. I was busy doing other work, so I wasn't surprised. I finally shut it down after another three hours. I assume when you noticed that it stopped adding to the log file that you paused it in the debugger and saw where it hung up.  Was Inventor still responding before pausing?

0 Likes
Message 20 of 20

Maxim-CADman77
Advisor
Advisor

@jjstr8 Yes - Inventor stays responding after the log stopped updating.

Please vote for Inventor-Idea Text Search within Option Names

0 Likes