Cancel ElastixImageFilter.Execute()

Hi, I have a program that sets up and calls ElastixImageFilter.Execute() from SimpleITK library when the user clicks the “Apply” button, it works fine, however sometimes the user changes mind and wants to cancel the on-going execution of the registration instead of waiting for up to 2 minutes then discard the output. I see in Slicer3D it is achieved by calling elastix.exe in a new process and killing the process when user clicks “Cancel”.
Calling elastix.exe in a new process in my program is not an option (running over mapped network drive, UAC warning, etc), simply killing my ElastixImageFilter.Execute() thread is also not good as it may leave large pieces of memory behind, so is there any other way to gracefully cancel/abort the long execution of ElastixImageFilter.Execute()? Thanks!

This is a little complex. You may look at the SimpleFilters 3DSlicer as an example. First by running it and the code.

ITK (SimpleITK) supports event such as progress, iteration and abort. An abort state can be set to a filter and when the filter updates the progress, an exception should be thrown and the execution aborted.

The SimpleFilters module run a filter in a separate thread, and correct handles the Python GIL so that the filters events and the Slicer handler can work together. This include the abort event.

You can see the code for reference:

The SimpleFiltersLogic class is the one handling the event logic. There is probably a base class could be refactored out.

The other question is does the Elastix filter handle the events correctly to report and receive events.

Thanks! I am using the C# version of itk.simple.ElastixImageFilter and the Execute() method is a synchronous call, so once I call myElastixFilter.Execute(), it will not return until it is done. I don’t see a way to call something like myElastixFilter.Abort() while it is executing. I don’t see anything like progress, iteration and abort in itk.simple.ElastixImageFilter’s API.