I am developing a C# add-in for Solidworks. I am debugging it from Visual Studio 2017. The .dll is registered properly, and everything works as it should. For debugging purposes, I set VS2017 to launch Solidworks after re-compiling .dll (Start External Program).
Now, if I set any method to run during the initialization of my add-in in the Solidworks, and it encounters an unhandled exception, I am returned back to the IDE with the exception printed out at the corresponding line of code, so I can debug and fix it, just like it should. However, if an exception occurs AFTER the add-in and Solidworks are loaded, Solidworks closes silently, and the debugging session in VS2017 ends. In other words, no unhandled exception message, no indication to what caused it.
I confirmed this by intentionally creating a method that should cause an unhandled exception (division by zero). If I call this method anywhere during the initialization of the add-in, it am returned back to my IDE with exception info being printed out. If I call this method anytime later (say, pressing button in the UI or on timer), debugging session ends silently and Solidworks closes.
I am not entirely sure if this is a Solidworks thing, or maybe Visual Studio IDE thing, but I hope that someone might have encountered a similar issue, and could suggest what to do.
I am not posting any code because I'm not sure what could be relevant here, but if you need anything to diagnose the problem, just let me know and I'll provide it.
SolidworksApi/macros
No worries, you can also use this technique with other frameworks as well or with standard sdk.
Thank you very much, Artem - and sorry for a late reply, I was traveling. Watching your videos made me realize that I forgot to set Solidworks.Interop.solidworks Embed Interop Types to False in the Reference properties of my project, which was caused that error. As for the original problem, using the SwAddInEx AND enabling all of the Common Language Runtime Exceptions in the Exceptions Window, fixed the problem.
Hi,
Answering your first question. I'm not exactly sure but the issue related to the way SOLIDWORKS loads .NET dlls. If you would try to reproduce this issue from the console application you will see that even when exception is raised from timer tick it will be treated as unhandled. I think SOLIDWORKS environment handles those cases differently and when ThreadException is handled it wraps it somehow - not really sure about this, as those are specifics of SOLIDWORKS.
Regarding your second question, if you can share the code - I can look at this. But you can view this video where I explain how to use this framework: Tutorial: Creating SOLIDWORKS Add-In from scratch (part 1) - YouTube , Tutorial: Creating SOLIDWORKS Add-In from scratch (part 2 - Property Pages) - YouTube , also documentation: SwEx - framework which simplifies the development using SOLIDWORKS API
Thanks,
Artem
So if I understand this correctly, without attaching the OnThreadException method to Application.ThreadException, the debugger treats all unhandled exceptions as first chance? Is that why it would stop debugging session without showing unhandled exception message? Why is it necessary to add OnThreadException method?
An unrelated question - I am still trying to use your code as an example to set up my own add-in. It is the first time I am using SwAddInEx library. I set everything up pretty much identically, but when I try to build, I get an error:
I am not sure why is ISwAddin mentioned at all (I used it before instead of SwAddInEx , but now all references to ISwAddin in my code are deleted). The SolidWorks.Interop.swpublished.dll is in the project references, exactly at the path that the compiler is looking for. Embed Interop Types is set to False, Copy Local is True. I know this is a bit outside the scope of the original question, but I was hoping you'd know what to do, since I've been comparing my code with yours for the better part of the day, and I'm still clueless.
There are 2 types of exceptions:
When code throws an exception at this point this is a first chance exception (i.e. it gives a chance for your code to catch it). If there is no try-catch block - this exception become unhandled.
When either of this exceptions happens, debugger stops and decides what to do with that. Usually for unhandled exceptions debugger would stop and show the exception details (something what you are looking for). This is not the case for the first chance exceptions as default options would not stop the debugger rather transfer the exception up the stack for handling.
For a quick test in your original add-in. You can put try-catch block on the division by zero block in your ConnectToSw function and you will see that debugger no longer stops and shows the error, because this exception was changed from unhandled to first chance (as it is handled now). You can also tell your debugger to break on specific first chance exception. So if in your original add-in (without any changes) if you would enable the following Exception setting in your Visual Studio you will notice that now debugger stops (even for the code run from the timer or button click - your original question)
I would strongly recommend to handle all unhandled exception with try-catch blocks as it may result in crashes and stability issues.
Thank you, Artem, the example you provided works (exception returns to IDE). I am still having trouble transferring it to my add-in (for some reason I'm getting a build error "system cannot find file specified" on SolidWorks.Interop.swpublished.dll, even thought it is added to References from the CodeStackEx folder provided by SwAddInEx), but I hope I'll fix that one. In the meantime, can you explain how this whole thing works?
Check attached add-in. Just set the external application path to SLDWORKS.exe and run. Let me know if that works and I will explain the reasons.
Solidworks 2018, we haven't updated here yet. No, I didn't use that Addin wizard, I followed the well-known AngelSix tutorials to build the link to Solidworks (specifically, these series: SolidWorks API (C# SolidDNA) - YouTube ).
What version of SOLIDWORKS? Are you using the SW Addin wizard in Visual Studio to create your add-in?