I am developing a .Net (windows form) application which will be called by a command from autocad.
The task is looking through all entities in the model and select specific polygons. This is sort of a heavy task, so I am having the UI freezed for minutes which is not suitable. Thus I tried run the task in seperate thread: starting a transaction, getting objects and check some of their properties.
Now, it works fine with one transaction , but if I use more than one transaction the app fails randomly. The stack shows error on this call:
".. Autodesk.AutoCAD.Runtime.Interop.CheckNull(IntPtr returnValue)
at Autodesk.AutoCAD.ApplicationServices.TransactionManager.StartTransaction()"
or when Autocad tries to Dispose the transaction object. ( I'm using USING block for strating transaction, therefore not calling Dispose method in the code.)
but these error never happenes in single-thread mode.
Can you please help me to understand the reason and find a solution ?
Or what else can one do to make UI responsive when a time-consuming task is running?
Thanks,
ali
Solved! Go to Solution.
Solved by dgorsman. Go to Solution.
Solved by chiefbraincloud. Go to Solution.
Calls into the AutoCAD .NET API from a separate thread are not safe, as you have discovered.
As far as the UI, check out these posts on Through The Interface:
http://through-the-interface.typepad.com/through_the_interface/2007/02/allowing_users_.html
http://through-the-interface.typepad.com/through_the_interface/2007/05/displaying_a_pr.html
http://through-the-interface.typepad.com/through_the_interface/2007/08/a-handy-net-cla.html
I think that the third link is pretty much a combination of the first two, but it has been a while since I read them, so I gave all three, just in case there were differences in the approach.
Are you running the calculations as you iterate through the objects? If so, consider harvesting the data from the objects in the drawing in a very lightweight loop (minimal filtering) and then passing the raw data into your calculations. You may be able to successfully spin off those calculations into multiple threads since they would not be dependant on AutoCAD.
Oh, hang on - just re-read your post. Calling a separate UI-based application outside of AutoCAD will cause all sorts of slowness. If at all possible, keep everything in-house to maximize speed. Trying to run drawing-dependant calculations outside of AutoCAD while the user can diddle inside the drawing can be *very* problematic as well. Very few professionally developed applications do this, and for good reason.
Many thanks guys,
I used the DoEvents method and its working fine.
Why dose it make faster if the application run the code inside autocad rather than windows-form app?
thnx