![](/img/trans.png)
[英]I want to execute code in C# after a specific function call in CAPL code
[英]Have code execute after call to async function
我有一個異步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()函數的更新代碼。
更改您的異步方法以返回任務,然后在您的main方法調用await(您的異步方法):
public async Task<T> Update(List<string> allNumbers, List<string> allValues)
在Main方法中
await Update(allNumbers, allValues);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.