Select to view content in your preferred language

Show progress when running a geoprocessing

2586
12
Jump to solution
08-30-2023 11:44 AM
HelenZhou
Frequent Contributor

Hello, I am working on an ArcGIS Pro SDK project, which includes running a geoprocessing tool. This tool runs 30+ seconds. Is there a good way to show progress which the tool is running?

I have found   ArcGIS.Desktop.Framework.Threading.Tasks.ProgressDialog. https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic10965.html#i-heading-example. When I use the example code in my application, I don't see the progress dialog shows up (not in debug mode).

here is the code module -

public static async Task<bool> ProcessSpatialJoin(string target_feats, string join_feats, string output_feats, string search_radius)
{

//https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic10957.html
var progDlg = new ProgressDialog("Running spatial join", "Cancel", 100, true);
progDlg.Show();
var progsrc=new CancelableProgressorSource(progDlg);
System.Windows.MessageBox.Show("Progress bar show");
var environments = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);

var parameters = await QueuedTask.Run(() =>
{
var join_operation = "JOIN_ONE_TO_ONE";
var match_option = "INTERSECT";  
var field_map = "";  

return Geoprocessing.MakeValueArray(target_feats, join_feats, output_feats, join_operation,

"KEEP_COMMON", field_map, match_option, search_radius);
});

GPExecuteToolFlags flags = GPExecuteToolFlags.AddOutputsToMap | GPExecuteToolFlags.GPThread;

var toolResult = await Geoprocessing.ExecuteToolAsync("SpatialJoin_analysis", parameters, environments, null, null, flags);
// dialog hides itself once the execution is complete
progDlg.Hide();
System.Windows.MessageBox.Show("Progress bar hide");
if (toolResult.IsFailed == true)
{
Geoprocessing.ShowMessageBox(toolResult.Messages, "GP Messages", toolResult.IsFailed ?
GPMessageBoxStyle.Error : GPMessageBoxStyle.Default);
}

return !(toolResult.IsFailed);
}

0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I fixed your snippet above.   I guess the main issue was that the  CancelableProgressor parameter for ExecuteToolAsync is missing.

    protected override async void OnClick()
    {
      var done = await ProcessSpatialJoin(@"Crimes", @"Portland Precincts", @"C:\Data\Interacting with Maps\Interacting with Maps.gdb\Crimes_SpatialJoin", "50 Yards");
      MessageBox.Show($@"Process done: {done}");
    }

    public static async Task<bool> ProcessSpatialJoin(string target_feats, string join_feats, string output_feats, string search_radius)
    {
      //https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic10957.html
      var progDlg = new ProgressDialog("Running spatial join", "Cancel");
      progDlg.Show();
      var progsrc=new CancelableProgressorSource(progDlg);
      //System.Windows.MessageBox.Show("Progress bar show");
      var environments = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);

      var parameters = await QueuedTask.Run(() =>
      {
        var join_operation = "JOIN_ONE_TO_ONE";
        var match_option = "INTERSECT";
        var field_map = "";

        return Geoprocessing.MakeValueArray(target_feats, join_feats, output_feats, join_operation,

        "KEEP_COMMON", field_map, match_option, search_radius);
      });

      GPExecuteToolFlags flags = GPExecuteToolFlags.AddOutputsToMap | GPExecuteToolFlags.GPThread | GPExecuteToolFlags.AddToHistory;

      var toolResult = await Geoprocessing.ExecuteToolAsync("SpatialJoin_analysis", parameters, environments, progsrc.CancellationTokenSource.Token, null, flags);
      //// dialog hides itself once the execution is complete
      //progDlg.Hide();
      //System.Windows.MessageBox.Show("Progress bar hide");
      if (toolResult.IsFailed == true)
      {
        Geoprocessing.ShowMessageBox(toolResult.Messages, "GP Messages", toolResult.IsFailed ?
        GPMessageBoxStyle.Error : GPMessageBoxStyle.Default);
      }
      return !(toolResult.IsFailed);
    }

I tried with release 3.0 as well and it worked with 3.0 too:

Wolf_0-1693896726995.png

 

View solution in original post

0 Kudos
12 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

If you are running add-ins from Visual studio it will not show progress dialog ( no matter Debug or Release mode) . Try to run it directly from ArcGISPro.exe executable.

0 Kudos
HelenZhou
Frequent Contributor

The ArcGIS Pro is not launched from Visual Studio. Thanks

0 Kudos
GKmieliauskas
Esri Regular Contributor

1. Try to delete line:

var progsrc=new CancelableProgressorSource(progDlg);

2. Try to change geoprocessing  flag by removing GPExecuteToolFlags.GPThread.

3. If your geoprocessing fails (invalid parameters and etc.) when you will not see progress dialog too

0 Kudos
HelenZhou
Frequent Contributor

@GKmieliauskas , Thanks. The tool is working. I just need to add a progress bar for the long geoprocessing time. 

I have tried to remove the code specified in item 1 and 2. Progress bar still doesn't display.

0 Kudos
GKmieliauskas
Esri Regular Contributor

Look at the @Wolf sample in ArcGIS Pro SDK thread

It uses CancelableProgressorSource as parameter for geoprocessing call

0 Kudos
HelenZhou
Frequent Contributor

Thanks again @GKmieliauskas. I have checked @Wolf sample. But, According to this ESRI documentation https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic9384.html There is no parameter of CancelableProgressor in the parameter list for Geoprocessing.ExecuteToolAsync

0 Kudos
HelenZhou
Frequent Contributor

Thanks for the link. I understand they are two methods with different parameters. My code still does not show progress bar after matching with the example. On the other hand, during running the geoprocess tool, the ArcGIS Pro is frozen until the tool is finished.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

This code worked for me:

 

      try
      {
        var progDlg = new ProgressDialog("Running Geoprocessing Tool", "Cancel");
        var progsrc=new CancelableProgressorSource(progDlg);

        // prepare input parameter values to CopyFeatures tool
        string input_data = @"C:\Data\Admin\AdminSample.gdb\TestArea";
        string out_workspace = ArcGIS.Desktop.Core.Project.Current.DefaultGeodatabasePath;
        string out_data = System.IO.Path.Combine(out_workspace, "TestArea2");

        // make a value array of strings to be passed to ExecuteToolAsync
        var parameters = Geoprocessing.MakeValueArray(input_data, out_data);

        // execute the tool
        await Geoprocessing.ExecuteToolAsync("management.CopyFeatures", parameters,
            null, new CancelableProgressorSource(progDlg).Progressor, GPExecuteToolFlags.Default);

        // dialog hides itself once the execution is complete
        progDlg.Hide();
      }
      catch (Exception ex)
      {
        MessageBox.Show (ex.ToString ());
      }

 

the difference is that i set the 'delayedShow' parameter for the ProgressDialog constructor to false (not specifying that parameter causes it to be set to false), and i removed the progDlg.Show() line.

0 Kudos