I'm working in UWP and need to call the UI thread, which I've done by wrapping the call in a Dispatcher.RunAsync(...) call. The code below doesn't compile, because:
Anonymous function converted to a void returning delegate cannot return a value
public async Task<RecentPlaysInfo> GetRecentPlayInfoAsync()
{
  await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  {
    // This code is on the UI thread: 
    IReadOnlyList<MediaTimeRange> list = meCurrentMedia.MediaPlayer.PlaybackSession.GetPlayedRanges();
    for (int i = 0; i < list.Count; i++)
    {
      // if some criteria on list[i] is met: 
      return new RecentPlaysInfo(...);
    }
  });
}
Brief aside: I tried isolating the Dispatcher.RunAsync(...) call so that it set a variable rather than going return, but the execution seemed to disappear into a blackhole and so I've found myself on this path, at least for now.
This thread helped me de-anonymize it (code below, note the additional return so that all paths return a value) but now the compile error is:
cannot convert from 'System.Func<Regal.Common.RecentPlaysInfo>' to 'Windows.UI.Core.DispatchedHandler'
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, (Func<RecentPlaysInfo>)(() =>
{
  // This code is on the UI thread: 
  IReadOnlyList<MediaTimeRange> list = meCurrentMedia.MediaPlayer.PlaybackSession.GetPlayedRanges();
  for (int i = 0; i < list.Count; i++)
  {
    // if some criteria on list[i] is met: 
    return new RecentPlaysInfo(...);
  }
                
  return null;
}));
I also read this (kaffekopp's answer) and this (Jon Skeet's answer) but I don't even pretend to fully understand them :(
I was able to code-up and call kaffekopp's modified DispatchToUI(...) method but that simply gives me:
cannot convert from 'System.Func' to 'System.Action'