0

Say you have 10 functions in C# that call each other. They all work fine. Later on the lowest function changes so it needs to call an api and await something. That means the function must be async. Now each function that calls that function must also await & be async.

Is there a way to break that?

As it is, I have to change all the functions that are related to that async functions into async functions. It's a hassle and I was wondering if there's something like the FutureBuilder for C#?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    Does this answer your question? [How to call asynchronous method from synchronous method in C#?](https://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c) – GSerg Feb 13 '23 at 14:01
  • It would probably help to provide a little more details about what you're trying to do, with a code sample. The general problem you're describing, where you have to change method signatures all the way up the call stack is a well-known inconvenience, but going "async all the way" is generally the best approach as every workaround carries risks. Are you asking about that general problem, or something like the specific problem FutureBuilder solves, where an item will be requested multiple times and you want to return a different object depending on whether a Task has been resolved? – StriplingWarrior Feb 13 '23 at 17:46
  • @GSerg Kinda yes, Will give it a deeper dive & try it on my own as that's the best way for me to understand something, thank you so much. – Shadi Aslan Feb 14 '23 at 05:29

1 Answers1

1

FutureBuilder is a way to build a UI that reacts to futures as they complete.

There's nothing built-in to .NET that does a similar thing, but it's not too hard to build your own or use something from a library. Library solutions include MvxNotifyTask from MvvmCross, ObservableObject.TaskNotifier from the .NET Community Toolkit / MVVM Toolkit v7 (previously known as NotifyTaskCompletion<T> in v6), or NotifyTask<T> in Nito.Mvvm.Async.

Most implementations are more or less derived from this old article of mine, but note that the sample code for that article has a bug when creating a NotifyTaskCompletion<T> from an already-completed task, so don't use it; use one of the implementations above or write your own instead.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Thanks for the suggestions, I already used MVVM community tool while learning some WPF, and I think it's what I am trying to do. Here's my dart code if you're interested in what I am trying to do in c#: [link](https://github.com/shadilios/code-samples/blob/main/future_cancel.dart) *hopefully I haven't messed it up, as I renamed a lot of things to make it more simple & far from my application structure. – Shadi Aslan Feb 14 '23 at 05:42