Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X
I have also attached one of the log files I get when I get this error popup.
This error is a classic software race condition, which is happening inside Mathcad's calculation engine. It's a timing issue where one part of the program tries to read a list of items while another part is simultaneously trying to change that same list.
The two key error messages tell the whole story:
Collection was modified; enumeration operation may not execute.
: This is like trying to count a list of items on a piece of paper while someone else is actively erasing and adding new items. The program stops to prevent a crash or data corruption.
The calling thread cannot access this object because a different thread owns it.
: This confirms it's a multi-threading problem. One process (a thread) is stepping on the toes of another, which is a common cause of race conditions.
Your setup, which involves a complex structure of linked worksheets with relative paths and automation via a Python script, is the perfect environment for this bug to surface.
When you open a Mathcad file, its calculation engine starts analyzing all the linked files ("includes" or "libraries") to figure out what needs to be calculated.
Your Python script, using MathcadPy
, is likely opening, reading, and possibly modifying these files very quickly.
This rapid file access from your script conflicts with Mathcad's own background process that is trying to manage the file dependencies. The two processes "race," and sometimes your script modifies the file list just as the calculation engine is trying to read it, causing the crash.
The randomness occurs because it depends on the precise millisecond timing of your computer's operations, network speed (since you're on a corporate network), and file I/O, which can change every time you run the script.
Here are several strategies, from simplest to most involved, to resolve this issue.
This is the most likely and easiest fix. The script is probably overwhelming Mathcad's file handling. Add a short delay after opening a file and before triggering a calculation or saving.
In your Python script, add a small sleep after each significant Mathcad operation (like opening a file):
import time
# Example within your script loop
mc.open('C:\\path\\to\\your\\file.mcdx')
time.sleep(2) # Pauses the script for 2 seconds
# Now proceed with getting values, calculating, etc.
A 1 or 2-second pause is often enough to let Mathcad's engine stabilize before you ask it to do the next thing.
Your environment variables show you're using OneDrive and a corporate domain (goldlnk.rootlnka.net
). Network latency and file syncing can make these timing issues much worse.
Before running your script, try copying the entire folder structure from the network/OneDrive location to a simple, local folder (e.g., C:\Mathcad_Temp\
). Run the script on the local files. This eliminates network lag as a variable.
Deeply nested relative paths (../../library/file.mcdx
) can sometimes be harder for applications to track reliably.
Use the "Include Worksheet" Feature: Instead of just referencing variables from other files, consider using Mathcad's dedicated Insert > Include Worksheet
feature. It can sometimes be more stable.
Flatten Your Directory: If possible, reduce the number of parent directory (..
) lookups. A flatter structure might be easier for the calculation engine to manage.
You are on version 10.0.0.0
. This kind of bug is often fixed in subsequent maintenance releases (e.g., 10.0.1.0 or similar). Check the PTC support website for any available updates for Mathcad Prime 10, as they may have already patched this specific race condition.
If none of the above solutions work, this is a bug within Mathcad Prime. Provide PTC support with the exact log file you've shared. This detailed information is exactly what their developers need to identify and fix the root cause in a future update.
Thank you so much. This is very helpful.
I will try to slow down the scripts to allow for Mathcad to stabilize before trying to access the data.
As an update to this thread - I was able to debug our code a little further and I find that the Python scripts are causing Mathcad to crash when we issue the command worksheet.Synchronize().
I added a print statement before and after the synchornize() command and the code never gets to the print statement after the command, so it is failing during the synchronize API call.
I found some information on the Mathcad API for worksheets here
It seems like there is an option to change the default calculation time via the DefaultCalculationTimeout(arg) API call, but there isn't a lot of information on this. I also tried issue the command worksheet.DefaultCalculationTimeout(-1) so that the sheet waits indefinetly. I assume that this would make the code wait until the sheet completes synch/recalculation to fix the problem with the Python scripts running too fast before Mathcad finishes calculating, but is that a wrong assumption? Additionally, I seem to get errors "Exception: 'int' object is not callable" due to adding this line in our code. Am I doing something wrong?
There is little documentation/help for the Mathcad API so any help is appreciated.
This got too complicated for me so I asked an AI engine for help. Attached is a pdf. See if it is of any assistance.
The AI doesn't quite get it right, but it is correct in that the Exception in python is giving you a clue as to the problem. "Exception: 'int' object is not callable" is to be expected, as worksheet.DefaultCalculationTimeout is a property, not a function. I.e. it should be operated as follows:
worksheet.DefaultCalculationTimeout = -1
An alternate approach to this could be to pause calculation after opening the document, make the modifications to the file that you need to, resume calculation, then keep trying to extract the output values until the timeout error stops populating:
worksheet.PauseCalculation()
# e.g. do stuff
worksheet.set_real_input("input", 1)
worksheet.ResumeCalculation()
error_code = 8 # start with an error code = Timeout
while error_code == 8:
value, units, error_code = worksheet.get_real_output("out")
# This loop will be delayed by the calculation timeout settings (and the calculation time of the sheet)
if error_code == 8:
print("Waiting for calculation")
print(value)
I have found that the crashes do not seem to be specific to the Python scripts, but they even occur just opening Mathcad Prime normally and issuing the recalculate command (F5). I will get the same error popup, as well as the traceback.log file.
I am not sure if the traceback.log file is any helpful to anyone. It doesn't mean a lot to me.
Do you have Multithreading enabled (Calculation Tab -> Calculation Options)?
If so, try disabling it and retry the recalculate command.
I did not / never had Multithreading enabled in the Calculation optionds.