1

All the 3rd partly API calls now use Async methods. I do not want to use them async. I want to convert them to syncronous because I do not care about a few seconds, and I can not seem to do async all the way up on this legacy program.

I just want to do this:

string token = api.GetTokenAsync().Result;

But I see lots of complaints that Wait() and .Result can deadlock!

This seems EXTREMELY complicated situation now, and I would like a very simple technique to call it syncronously. There must be simple techniques for this because there are so many scenarios where of course you have to wait for the result anyway. So making it async is kind of redundant if you are OK with the UI locking, as most of the time the customers will be waiting anyway so its no drama locking the UI until its finished.

update, I found this answer the best: Task keeps waiting for activation

  var task= Task.Run(async () =>
                   {
                       return await Api.LoginAsync( tbUsername.Text, tbPassword.Text);
                   });
         _Token  = task.Result; 
Luke
  • 194
  • 3
  • 10
  • `Result` and `Wait` can lead to deadlock if you use them in combination with async-await. In other words if you [mix sync and async code](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). If ALL of your code is sync then you don't need to fear deadlocks. – Theodor Zoulias Dec 30 '19 at 01:26
  • It won’t deadlock if you spawn a thread just for that async, as in my own answer given. – Luke Jan 09 '20 at 07:01

0 Answers0