c# - How to get returned value of async Task methdoName()? -
I am trying to get the return string of my method but the problem is that I do not know how I get it I can This is my code from Program.cs This is my code from the Saba Controller CS Currently you are just calling Now, this will work in an console app because there is no On the one hand, it appears to be stored in the public async Tasks & lt; String & gt; Log In (String Username, String Password, String Site) .
Fixed zero main (string [] args) {Var username = "Leonel.Sarmiento"; Var Password = "Welcome"; Var site = "QADBSite"; Var url = "na1.sabacloud.com"; Console Customizer.Spinner Spinner = new ConsoleCustomizer.Spinner ("+", "x", "+", "x"); Console Customizer.TypeWriter TypeWriter = New Console Customizer.TypeWriter (15, 150); Console Customizer.Alerts Alert = New Console Customizer.Alerts (); Alert. Type ("notification", "HOST URL:", blank); TypeWriter.WriteLine (@ "http: \\" + URL); Alert. Type ("notification", "USERNAME:", tap); TypeWriter.WriteLine (username); Alert. Type ("notification", "password:", blank); {TypeWriter.Write ("*") for (Var i = 0; i & lt; password.Length; i ++); } Console. "Line" (""); Saba controller saba = new subsystem controller (url); // var certificate = saba.Login (username, password, site). Wait (); Saba.Login (username, password, site). Wait (); Console.Read (); }
Public async Tasks & lt; String & gt; Login (string username, string password, string site) {(using client (var client = new HTTP) (client.BaseAddress = new URI ("https: //" + + + + + + + + HostURL + / ") ; client.DefaultRequestHeaders.Accept.Clear (); client.DefaultRequestHeaders.Accept.Add (New MediaTypeWithQualityHeaderValue ("App / Jason"); Client.DefaultRequestHeaders.Add ("user", user name); Client.DefaultRequestHeaders.Add ( "Password", password); Client.DefaultRequestHeaders.Add ("Site", Site); // HTTP GET: Saba / API / Login HttpResponseMessage Feedback = client.GetAsync ("Saba / API / Login") Tranjar; if (response.IsSuccessStatusCode) {Sabamodel saba = response is awaiting.ReadAsink & lt; Saba Model & gt; (); Saba certificate = Saba certificate;}} Saba certificate return;}
() - which Will block the work until it's done, but you will not return the return value. If you use the
result property instead, which will block you and then give you results:
string certificate = saba.login (username, password, Site). result ;
synchronization context ... which means that async method Continuity will be executed on the Thread Pool Thread If you use the same code from the WinForms UI Thread (for example), then you end up with a deadlock - the UI thread will be waiting to complete the task , But as long as it can not be found on UI, some more code conforms Thread for filing.
saba certificate and
sba model in the
sba controller , but it is not clear It should do this.
Comments
Post a Comment