How to get returned value of async Task<string> method name()?

随声附和 提交于 2019-12-04 03:40:05

Currently you're just calling Wait() - that will block until the task completes, but won't give you the return value. If you use the Result property instead, that will block and then give you the result:

string certificate = saba.Login(username, password, site).Result;

Now, that will work in a console app because there's no SynchronizationContext... which means continuations in the async method will be executed on a thread pool thread. If you use the same code from a WinForms UI thread (for example) then you'd end up with a deadlock - the UI thread would be waiting for the task to complete, but the task couldn't complete until it got onto the UI thread to execute some more code.

As an aside, this appears to be storing SabaCertificate and SabaModel in the SabaController, but it's not obvious that it should be doing that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!