Select to view content in your preferred language

What can be included in a QueuedTask

242
2
Jump to solution
06-24-2024 10:50 PM
mody_buchbinder
Occasional Contributor III

In my application every layer have it's own form with some information.

The user have a table where each record have a layer name. When the user double click a record the layer's form should be opened.

There are two checks before the form is opened. The layer must be in the TOC and no other layer form is open.

If any of these conditions exists I show MessageBox and do not open the form.

The check of the layer in TOC must be in QueuedTask since I need to get the database name (in the TOC the layer have the alias name).

So I have to do check 1, possible show message box and then do check 2.

Putting everything into one QueuedTask have a problem with messagebox. It jump to the second test while the messagebox is still on the screen.

Any good way to do it?

1 Solution

Accepted Solutions
CharlesMacleod
Esri Regular Contributor

mody, in addition to what Uma says below, it is also possible to split your workflow into multiple steps where each step is within the context of a QueuedTask and, in between steps, u can show a modal UI to the user. The key is to "await" each step wrapped within a QueuedTask like so:

 

internal class Button1 : Button {

protected async override void OnClick()
{
  //step 1 - await it before proceeding to step 2
  var ok = await QueuedTask.Run(() => {
    //do work
    return true;
  }
   //Back on the UI
   if (!ok) {
      MessageBox.Show("......");
      return; //exit perhaps
   }

   //continue on to step 2
   ok = await QueuedTask.Run(() => {
      ....
   //Back on the UI
   if (!ok) {
     MessageBox.Show("......"); 

   //continue on to step 3
   ok = await QueuedTask.Run(() => {
      ... //and so-on

 

The only caveat here is that each time that u return to the UI, there is an opportunity for other "processes/workflows" to use the QueuedTask in-between your steps so, ideally, u want to capture any state u need upfront (such as the layer u r examining into a local variable and so on)

View solution in original post

2 Replies
UmaHarano
Esri Regular Contributor

There are few ways you can accomplish this.

One way is to - use Microsoft's Dispatcher class CheckAccess/BeginInvoke pattern to update UI controls, from within a QueuedTask background thread.

This pattern is documented quite a bit on Microsoft's site. The Pro SDK team also presented this pattern relating to Pro AddIn development in this Esri Dev Summit 2024 session. This concept is talked about at around 34 minutes in.

ArcGIS Pro SDK for .NET: Asynchronous Programming for Addins 2 

 

 

 

 

0 Kudos
CharlesMacleod
Esri Regular Contributor

mody, in addition to what Uma says below, it is also possible to split your workflow into multiple steps where each step is within the context of a QueuedTask and, in between steps, u can show a modal UI to the user. The key is to "await" each step wrapped within a QueuedTask like so:

 

internal class Button1 : Button {

protected async override void OnClick()
{
  //step 1 - await it before proceeding to step 2
  var ok = await QueuedTask.Run(() => {
    //do work
    return true;
  }
   //Back on the UI
   if (!ok) {
      MessageBox.Show("......");
      return; //exit perhaps
   }

   //continue on to step 2
   ok = await QueuedTask.Run(() => {
      ....
   //Back on the UI
   if (!ok) {
     MessageBox.Show("......"); 

   //continue on to step 3
   ok = await QueuedTask.Run(() => {
      ... //and so-on

 

The only caveat here is that each time that u return to the UI, there is an opportunity for other "processes/workflows" to use the QueuedTask in-between your steps so, ideally, u want to capture any state u need upfront (such as the layer u r examining into a local variable and so on)