Cancel all QueuedTask.Run() tasks with Exit Button

1235
2
06-14-2018 11:46 AM
EmilyBunse
New Contributor

Hi, 

I am updating an ArcMap Add-In written in VB.NET for use in ArcGIS Pro, so I've applied multi-threading to the project and have utilized the Await QueuedTask.Run() method for a variety of tasks. However, I'm very new to asynchronous programming.

The problem is that the ArcMap Add-In has an "Exit" button which should stop all running Add-In code when it is clicked. I'd like to keep this because the tool does so much that I could see a user wanting to stop it partway through and reset the parameters. Originally, exiting was done with 'Application.Exit()' within that button's sub, but that closes down the Add-In AND ArcGIS Pro which would be very frustrating to users. Currently, I can close the form but the background threads continue executing until completion. 

I've tried using 'Environment.Exit()' with no result. Then I've been trying to make CancellationTokenSource work through a combination of the examples in the following links: Cancel an Async Task or a List of Tasks (Visual Basic) | Microsoft Docs and https://community.esri.com/message/625457?commentID=625457#comment-625457

I placed:

If cts IsNot Nothing Then

cts.Cancel()
End If

in the Exit Button sub in the form class and cts is a public variable in the solution which is added as a parameter in ExecuteToolAsync() as 'cts.Token', but it doesn't seem to work to cancel any geoprocessing tools during Debugging when the "Exit" button is clicked. 

One last solution I've been exploring trying is how/if I should be using ArcGIS.Desktop.Framework.Threading.Tasks.CancelableProgressorSource: ProSnippets Framework · Esri/arcgis-pro-sdk Wiki · GitHub to cancel the threads and how that might work with the exit button .clicked event. 

 

Basically I'm asking if there is a way of cancelling all QueuedTask.Run() threads and shutting down an Add-In without closing the ArcGIS Pro application as well? 

Any ideas or insight would be much appreciated! 

0 Kudos
2 Replies
CharlesMacleod
Esri Regular Contributor

Hi Emily,

Progress dialog boxes are disabled when debugging via Visual Studio. That might be why you are not seeing the cancelable progressor. Try running your add-in outside of the debugger.

Note: Dockpanes are not actually destroyed when you close them (I assume you mean "Dockpane" when you say "form"?) - they are hidden. "Opening" them just makes them visible again.

FYI, if you haven't had a chance to look at it yet - this document goes into the gory details of cancelation: ProConcepts Framework#progress-and-cancelation....(I also add the link to this comment for other readers of your thread)

0 Kudos
EmilyBunse
New Contributor

Hi Charles, 

Thank you for your quick reply and suggestions- apologies for my delayed response. 

I am referring to a 'System.Windows.Forms.Form' which the ArcGIS Pro Add-In opens. I've tried to keep the workflow as similar to the ArcMap Add-In as I can, so I kept the form and workflow largely as-is. The Add-In creates products which help geologists create a cross-section beginning with the user defining a profile line.

The workflow goes: 1.) the user clicks a button on a custom tab on the ribbon- this activates the sketch tool where they draw a transect line. 2.) If they are satisfied with this line for the profile a form pops up which allows the user to fill out many options for use (e.g. in which field in the table are the geologic units stored, what units should the outputs be in- meters or feet, etc.). 3.) They then click the 'create profile' button which launches asynchronous tasks which create the feature classes/shapefiles and populate them with data.

There is an 'exit' button on this form which needs to exit all execution of the code so that the geologist can reset the parameters and run the tool again or do other tasks in Pro. 

Upon review of your suggested link, this looks like what I should be doing at each async task I create, but I'm a little confused about how to set the progressor.CancellationToken as Cancelled and how to link this with the code handling the exit button. I'm also concerned that this will only close the current task and the others will continue once the task it is called on is cancelled. 

I also tried QueuedTask.Shutdown(0) today in the sub for handling the exit button. This does stop code execution and allows the form to close, but then the ArcGIS Pro GUI is locked up and stuck on sketch tool.

0 Kudos