Exception thrown ESAPI v.11

May 23, 2014 at 12:09 AM
I have written a simple script that iterates through the Courses and PlanSetups of a patient, and displays their ids on screen.

For some reason, an exception is being thrown each time I run this script. I can't figure out why this is happening.

Any one had similar experience? Or could help me fix this ?


Here is the stack trace:
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.   at NPlanSetup.VerifyZeroDosePixels(MPlanSetup* , CStringT<char\,StrTraitMFC_DLL<char\,ATL::ChTraitsCRT<char> > >* )
   at VMS.TPS.Common.Model.PlanSetup.CheckPlanIntegrity() in b:\va\core\tps.net\common\vms.tps.common.model\plansetup.cpp:line 115
   at VMS.TPS.Common.Model.API.PlanSetup..ctor(IPlanSetup impl) in B:\VA\Core\TPS.NET\Common\VMS.TPS.Common.Model.API\CustomClasse
s\PlanSetupCustom.cs:line 17
   at VMS.TPS.Common.Model.API.Course.<get_PlanSetups>b__0(IPlanSetup v) in B:\VA\Core\TPS.NET\Common\VMS.TPS.Common.Model.API\Cou
rse.cs:line 84
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at VMS.TPS.Common.Model.API.Course.get_PlanSetups() in B:\VA\Core\TPS.NET\Common\VMS.TPS.Common.Model.API\Course.cs:line 84
   at MercatorDaemon.Program.FetchCourseAndPlanInfo(Application app, String mrn, List`1& courseInfo) in c:\Users\shashankbhatia\Do
cuments\Code\C#\mercatordaemon\trunk\MercatorDaemon\MercatorDaemon\Program.cs:line 96
   at MercatorDaemon.Program.Main(String[] args) in c:\Users\shashankbhatia\Documents\Code\C#\mercatordaemon\trunk\MercatorDaemon\
MercatorDaemon\Program.cs:line 37
And here is the section of Main method:
                    // create the application - passing (null,null) forces a manual sign in to Aria
                    using (Application app = Application.CreateApplication(username, password))
                        response = FetchCourseAndPlanInfo(app, patientMRN, out courseAndPlans);

                catch (Exception e)
Here is the actual script:
 public static string FetchCourseAndPlanInfo(Application app, string mrn, out List<MercatorDaemon.CourseInfo> courseInfo)
            string response = "OK";
            Console.WriteLine("Opening patient MRN = " + mrn);
            Patient patient = app.OpenPatientById(mrn);
            // export patient data
            courseInfo = new List<CourseInfo>();

            if (patient != null)
                if (patient.Courses.Any())

                    foreach (var course in patient.Courses)
                        CourseInfo thisCourse = new CourseInfo();
                        thisCourse.courseId = course.Id;

                        if (course.PlanSetups.Any())
                            thisCourse.planIds = new List<String>();
                            foreach (var plan in course.PlanSetups)
                                if (!String.IsNullOrEmpty(plan.Id))
                                    Console.WriteLine("Plan id = " + plan.Id);

                response = "No patient found";
            return response;
Jun 26, 2014 at 1:03 PM
After further investigation we found this was caused by use of multiple threads in an ESAPI standalone executable.

You can access the scripting API with only a single thread. Trying to access it with another thread will cause a crash like this.

The Eclipse Scripting API Reference Guide states:

"Use a single-threaded apartment (STA) as the COM threading model of the executable. The Eclipse Scripting API must only be accessed from a single thread that runs in the default application domain. For more information about threading and application domains, refer to Microsoft documentation"

Rex Cardan’s article may help.
Jun 26, 2014 at 9:43 PM
I am sorry If I had confused you.

The exception I reported in this thread, occurs even when accessing ESAPI through single Main thread. And I have used [STAThread] with the Main method.

If you read the above post, I have mentioned "And here is the section of Main method"
And have provided the code that calls ESAPI Application.

As described earlier, the application starts, it reads the names of first few courses and plans and then eventually throws an exception.


Any help is really appreciated.
Jul 8, 2014 at 4:30 PM
So far we have seen that if 32 bit standalone executables are created (which we recommend NOT doing in the reference guide), you can run out of memory. We have not been able to reproduce this sort of problem with a 64 bit standalone executable.