Can Eclipse API be used in multiple thread application?

Oct 14, 2014 at 2:44 PM
We have Eclipse API V11. We are working on a project that needs to extract treatment plan data from Eclipse API when clients sent a request via network (it is a client-server application). We tried to create an API instance inside the function public void ReadCallback(IAsyncResult ar). The approach is when a listener gets a request " "<EclipseAPI>", it collects PatientID, CourseID, and PlanSetupID from the clients, then create an API instance to extract the patient's treatment plan data. Unfortunately, it fails - the application automatically exits without any error message is caught ( try... catch.. statement does not work).
The following statement is used to create an API instance:
__Lazy<ESAPIApplication> _instance = new Lazy<ESAPIApplication>(() => new ESAPIApplication());
__Note: we use it in a Window Form application (collecting PatientID, CourseID, and PlanSetupID from a window form), it works fine.
We are very appreciated your help/guidance/clues to solve the problem.

Oct 14, 2014 at 3:30 PM
Try catch will not catch COM exceptions. I would bet the exception is happening because you are trying to create an instance of the App on a separate thread (which is not a STAThread). The API is not friendly when you try to get asynchronous. I did post a helper on how you can do asynchronous calls to the API but I would suggest just staying synchronous until they add support for pure .NET 4.5 async await callbacks.

Would love to see in the future something like.

var success = await Application.Login("username", "passsword");
var dvh = await patient.Get..

This would free up your UI to stay fluid when making time consuming calls to the DB.
Oct 14, 2014 at 4:30 PM
Hi Rexcardan,
Thanks for the quick response. Where did you post a helper on how we can do asynchronous calls to the API ? I'd like to learn it, and hope it can help us to solve the problem now.


Oct 14, 2014 at 4:53 PM
It's on the homepage listed like:

[12/24/2013] See Eclipse Scripting Tips and Tricks, added a new tip for accessing image voxel data
[12/10/2013] Rex added Getting Asynchronous with ESAPI.
[11/18/2013] See Eclipse Scripting API FAQ, added new FAQ for getting help with scripting.
Oct 14, 2014 at 11:08 PM
I wrote a similar application script that serves as a file server. It waits asynchronously for clients to connect, and serves as a file transfer server, but supplying the client requested plan file in DCM format.

I am supplying some snapshots of how I was able to start the script on a new thread:
switch (fileServer.actionCommand)
                                case "LOGIN":
                                    Thread thread = new Thread(() => Program.ExtractCourseAndPlanInfo());


                                case "EXPORT":
                                    GenericExporter planExporter = new GenericExporter(patientMRN);

                                    Thread newthread = new Thread(() => Program.ExportDicomPlan(planExporter));

                                case "EXIT":
                                    keepRunning = false;


Notice the command thread.SetApartmentState(ApartmentState.STA);

This makes the thread a STAT thread, and then the script runs fine.

If you are unable to see the error when you run the application, try launching your application from command prompt. When the application exits with an error, you will see some error message printed in the prompt.

Hope this helps.