AsyncPresentationEvent: how to know what Task to return?

88
1
Jump to solution
01-22-2021 02:33 PM
KirkKuykendall1
Occasional Contributor II

Hi -

How do I figure out what to return in a Task when handling an AsyncPresentationEvent?

For example this sample shows Task.FromResult(0) being returned, how was this (0) determined?

protected void subEditEvents()
{
  //subscribe to editcompleted
  var eceToken = EditCompletedEvent.Subscribe(onEce);
}

protected Task onEce(EditCompletedEventArgs args)
{
  //show number of edits
  Console.WriteLine("Creates: " + args.Creates.Values.Sum(list => list.Count).ToString());
  Console.WriteLine("Modifies: " + args.Modifies.Values.Sum(list => list.Count).ToString());
  Console.WriteLine("Deletes: " + args.Deletes.Values.Sum(list => list.Count).ToString());
  return Task.FromResult(0);
}

 

Thanks, Kirk

 

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

If you hover over the EditCompletedEvent.Subscribe method in Visual Studio (or use the Pro SDK API reference) you find the following parameters for the Subscribe method:

 

public static SubscriptionToken Subscribe( 
   Func<EditCompletedEventArgs,Task> action,
   bool keepSubscriberAlive
)

 

This shows that the first parameter is a delegate function that has an input parameter of the type 'EditCompletedEventArgs' and returns a class of the type System.Threading.Tasks.Task  .  The Task class is derived from the 'Task <TResult> class' .  Both variations of the Task class represent a single operation that usually executes asynchronously, however, whereas 'Task<TResult>' returns a value (of the type TResult), 'Task' doesn't return a value.  Consequently your delegate above 'onEce' has to return an instance of the Task class and the way to do this is to use the 'Task.FromResult<TResult>(TResult)' method and since TResult is not required you can specify either null or 0 as the parameter.  FromResult returns an instance of the task class that you need as your return parameter.

View solution in original post

1 Reply
Wolf
by Esri Regular Contributor
Esri Regular Contributor

If you hover over the EditCompletedEvent.Subscribe method in Visual Studio (or use the Pro SDK API reference) you find the following parameters for the Subscribe method:

 

public static SubscriptionToken Subscribe( 
   Func<EditCompletedEventArgs,Task> action,
   bool keepSubscriberAlive
)

 

This shows that the first parameter is a delegate function that has an input parameter of the type 'EditCompletedEventArgs' and returns a class of the type System.Threading.Tasks.Task  .  The Task class is derived from the 'Task <TResult> class' .  Both variations of the Task class represent a single operation that usually executes asynchronously, however, whereas 'Task<TResult>' returns a value (of the type TResult), 'Task' doesn't return a value.  Consequently your delegate above 'onEce' has to return an instance of the Task class and the way to do this is to use the 'Task.FromResult<TResult>(TResult)' method and since TResult is not required you can specify either null or 0 as the parameter.  FromResult returns an instance of the task class that you need as your return parameter.

View solution in original post