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.
If cts IsNot Nothing Then
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!