如何从async方法获取button_ClickEvent中的字符串
我们有以下代码:
public static CookieContainer cookies; public static HttpWebRequest GetNewRequest(string targetUrl, CookieContainer SessionCookieContainer) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUrl); request.CookieContainer = SessionCookieContainer; request.AllowAutoRedirect = false; return request; } public async static Task MakeRequest(HttpWebRequest request, CookieContainer SessionCookieContainer, Dictionary parameters = null) { HttpWebResponse response; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5Accept: */*"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.CookieContainer = SessionCookieContainer; request.AllowAutoRedirect = false; if (parameters != null) { request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; string s = ""; foreach (KeyValuePair pair in parameters) { if (s.Length == 0) { s = s + string.Format("{0}={1}", pair.Key, pair.Value); } else { s = s + string.Format("&{0}={1}", pair.Key, pair.Value); } } byte[] bytes = Encoding.UTF8.GetBytes(s); using (Stream stream = await request.GetRequestStreamAsync()) { stream.Write(bytes, 0, bytes.Length); } } request.Method = "GET"; response = await request.GetResponseAsync() as HttpWebResponse; SessionCookieContainer.Add(response.Cookies); while (response.StatusCode == HttpStatusCode.Found) { response.Close(); request = GetNewRequest(response.Headers["Location"], SessionCookieContainer); response = await request.GetResponseAsync() as HttpWebResponse; SessionCookieContainer.Add(response.Cookies); } return response; }
我用这个函数的一些方法(例如)
async Task login(string url, string id, string pw) { ///code... }
我的问题是:如果我想在buttonclick(object sender, EventArgs e)
获得结果,我该怎么办?
我试过这个但是不行不通:
private void buttonclick(object sender, EventArgs e) { string htmlPage=login(url, id, pw); }
编辑
我已经解决了在private
和void之间添加async
并在login(bla bla)
前添加await
的问题login(bla bla)
虽然以下是一个明显的解决方案,但它有隐藏的捕获:
private async void buttonclick(object sender, EventArgs e) { string htmlPage = await login(url, id, pw); }
这是一个稍微改进的版本:
Task _pendingLogin = null; private async void buttonclick(object sender, EventArgs e) { if (_pendingLogin != null) { MessageBox.Show("Login pending..."); return; } try { _pendingLogin = login(url, id, pw); string htmlPage = await _pendingLogin; MessageBox.Show("Logged in: " + htmlPage); } catch(Exception ex) { MessageBox.Show("Error in login: " + ex.Message); } _pendingLogin = null; }
您的登录方法是Async。 简单来说,异步方法在调用时不会返回结果值,而是异步方法返回TResult的任务(登录方法的字符串任务)。 Task是一种特殊类型,它代表您计算结果值的承诺。 为了得到结果,你应该:
试试这个:
上述就是C#学习教程:如何从async方法获取button_ClickEvent中的字符串分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
private async void buttonclick(object sender, EventArgs e) { string htmlPage = await login(url, id, pw); }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1005982.html