Nowhere in the API documentation are Threads discussed. Notscrew threads, operating system threads. Since the API is designedto the lowest common denominator (Visual Basic) which barely hasthe concept of threads, it seems to have been overlooked.
I've spent the better part of two weeks wrestling with a problemdue to this. I have a callback handler that responds to custom menuitems that my add-in has created. The handler works great, providedthe code I want to execute (when a menu item is selected) isexecuted on the same thread as the callback.
If I execute the code on a separate thread, the results vary. Somecalls succeed, others fail. A prime example is the ISelectByRay()function. With identical parameters, the code would fail on onethread and not on another.
Two of the parameters of this function are arrays of doubles. WhenCOM marshalls a pointer to an array from one thread to another,only the first element is kept intact. Hence a typical COM functionthat accepts an array also has a parameter indicating the size ofthe array passed, so that COM can marshall all the array elementsproperly. Since ISelectByRay doesn't get passed any size info, theY and Z coordinates from the two double arrays are garbage and thecall fails.
If the call isn't marshalled across threads, the pointer is valid,the Y and Z elements are found and the call succeeds.
So, if you are doing COM add-ins, and you want to use threads, becareful using any API method that has array pointers forparameters.
SolidworksApi macros







I've spent the better part of two weeks wrestling with a problemdue to this. I have a callback handler that responds to custom menuitems that my add-in has created. The handler works great, providedthe code I want to execute (when a menu item is selected) isexecuted on the same thread as the callback.
If I execute the code on a separate thread, the results vary. Somecalls succeed, others fail. A prime example is the ISelectByRay()function. With identical parameters, the code would fail on onethread and not on another.
Two of the parameters of this function are arrays of doubles. WhenCOM marshalls a pointer to an array from one thread to another,only the first element is kept intact. Hence a typical COM functionthat accepts an array also has a parameter indicating the size ofthe array passed, so that COM can marshall all the array elementsproperly. Since ISelectByRay doesn't get passed any size info, theY and Z coordinates from the two double arrays are garbage and thecall fails.
If the call isn't marshalled across threads, the pointer is valid,the Y and Z elements are found and the call succeeds.
So, if you are doing COM add-ins, and you want to use threads, becareful using any API method that has array pointers forparameters.
SolidworksApi macros
