本文介绍了我怎样才能从异步方法button_ClickEvent得到一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的家伙,我有以下code:
I guys I have the following code:
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<HttpWebResponse> MakeRequest(HttpWebRequest request, CookieContainer SessionCookieContainer, Dictionary<string, string> 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<string, string> 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;
}
我使用这个功能的一些方法(例如)
I use this functions some method(for example)
async Task<string> login(string url, string id, string pw)
{
///code...
}
我的问题是:如果我想获得 buttonclick结果(对象发件人,EventArgs五)
我该怎么办
我也试过,但这么想的工作:
I have tried this but dosen't work:
private void buttonclick(object sender, EventArgs e)
{
string htmlPage=login(url, id, pw);
}
修改
我已经解决了的私人
和无效的,并添加等待添加问题
在异步
登录(BLA BLA)
I' ve solved the problem adding async
between private
and void and adding await
before login(bla bla)
推荐答案
而下面是一个显而易见的解决方案,它具有隐蔽捕获:
While the following is an obvious solution, it has hidden catches:
private async void buttonclick(object sender, EventArgs e)
{
string htmlPage = await login(url, id, pw);
}
- 如果
登录
抛出异常?它会去不可观测,很可能会导致应用程序崩溃。 - 如果用户点击一个按钮两次,而previous异步登录操作已经在进行什么?你肯定不想要两个悬而未决的登录。
- What if
login
throws an exception? It will go unobserved and most likely will crash the application. - What if user clicks a button twice, while the previous async login operation is already in progress? You certainly don't want two pending logins.
下面是一个稍微改进过的版本:
Here's a slightly improved version:
Task<string> _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;
}
这篇关于我怎样才能从异步方法button_ClickEvent得到一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!