我有一个异步void函数,可以执行几个curl调用。我有需要在该函数执行后执行的代码,因为在函数内部设置了变量。现在看来,代码是在异步功能之前执行的。有什么建议吗?
Update(allNumbers, allValues);
//result is a global variable that is set inside the Update function.
if (result.Contains("success"))
{
message.InnerText = "Result: " + result + "\r\nSuccessfully updated value";
}
else
{
message.InnerText = "Result: " + result + "\r\nError updating value";
}
async void Update(List<string> allNumbers, List<string> allValues){
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
string file = "";
HttpRequestMessage response = new HttpRequestMessage();
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromMinutes(30);
client.BaseAddress = new Uri('Web IP');
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
try
{
file = System.IO.File.ReadAllText(path + "user.xml");
response = new HttpRequestMessage(HttpMethod.Post, 'Web Extension 1');
response.Content = new StringContent(file, Encoding.UTF8, "application/xml");
await client.SendAsync(response).ContinueWith(responseTask =>
{
Uri uri = new Uri('Web IP' + 'Web Extension 1');
IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri).Cast<Cookie>();
foreach (Cookie the_cookie in responseCookies)
{
File.WriteAllText(path + "sessionid.txt", String.Empty);
using (var tw = new StreamWriter(path + "sessionid.txt", true))
{
// Create file with cookie information
}
}
});
}
catch (Exception ex)
{
using (var stw = new StreamWriter(path + "error.txt", true))
{
stw.WriteLine("Error with Login cURL Call - " + ex.ToString() + "\r\n---------------------------------------------------");
}
result = "Error with Login cURL Call -" + ex.ToString();
}
string id = "";
try
{
response = new HttpRequestMessage(HttpMethod.Get, 'Web Extension 2');
await client.SendAsync(response).ContinueWith(responseTask =>
{
string contents = responseTask.Result.Content.ReadAsStringAsync().Result;
//assign id value
});
}
catch (Exception ex)
{
using (var tsw = new StreamWriter(path + "error.txt", true))
{
tsw.WriteLine("Error with Load Config cURL Call - " + ex.ToString() + "\r\n---------------------------------------------------");
}
result = "Error with Load Config cURL Call - " + ex.ToString();
}
if (id != "")
{
for (int i = 0; i < allNumbers.Count; i++)
{
try
{
file = System.IO.File.ReadAllText(path + allNumbers[i] + ".xml");
response = new HttpRequestMessage(HttpMethod.Post, 'We Extension 3.1' + id + 'Web Extension 3.2');
response.Content = new StringContent(file, Encoding.UTF8, "application/xml");
await client.SendAsync(response).ContinueWith(responseTask =>
{
});
}
catch (Exception ex)
{
using (var stw = new StreamWriter(path + "error.txt", true))
{
stw.WriteLine("Error with Update cURL Call - " + ex.ToString() + "\r\n---------------------------------------------------");
}
result = "Error with Update cURL Call - " + ex.ToString();
}
try
{
file = System.IO.File.ReadAllText(path + "saveActivate.xml");
response = new HttpRequestMessage(HttpMethod.Post, 'Web Extension 4.1' + id + 'Web Extension 4.1');
response.Content = new StringContent(file, Encoding.UTF8, "application/xml");
await client.SendAsync(response).ContinueWith(responseTask =>
{
});
}
catch (Exception ex)
{
using (var stw = new StreamWriter(path + "error.txt", true))
{
stw.WriteLine("Error with Save and Activate cURL Call - " + ex.ToString() + "\r\n---------------------------------------------------");
}
result = "Error with Save and Activate cURL Call - " + ex.ToString();
}
try
{
response = new HttpRequestMessage(HttpMethod.Get, 'Web Extension 5.1' + id + 'Web Extension 5.2');
await client.SendAsync(response).ContinueWith(responseTask =>
{
result = "Value update was a success!";
});
}
catch (Exception ex)
{
using (var stw = new StreamWriter(path + "error.txt", true))
{
stw.WriteLine("Error with Load Config cURL Call - " + ex.ToString() + "\r\n---------------------------------------------------");
}
result = "Error with Load Config cURL Call - " + ex.ToString();
}
}
}
else
{
using (var tsw = new StreamWriter(path + "error.txt", true))
{
tsw.WriteLine("Error getting current SDM ID number.\r\n---------------------------------------------------");
}
result = "Error getting current SDM ID number.";
}
}
}
这是Update()函数的更新代码。
答案 0 :(得分:3)
更改您的异步方法以返回任务,然后在您的主要方法中调用await(您的异步方法):
public async Task<T> Update(List<string> allNumbers, List<string> allValues)
在Main方法中
await Update(allNumbers, allValues);