我正在使用新的HttpClient类将一些数据上传到服务器,因此我不得不使用像await / async这样的新运算符。我有以下问题:如何阻止调用者线程等待第一个文件上传,然后移动到下一个文件。
public async Task Upload(string filename)
{
HttpRequestMessage message = new HttpRequestMessage();
StreamContent streamContent = new StreamContent(new FileStream(filename, FileMode.Open));
message.Method = HttpMethod.Put;
message.Content = streamContent;
message.RequestUri = new Uri(webURI);
HttpResponseMessage response = await httpClient.SendAsync(message);
//I want to reach this point untill I will start to upload next file
if (response.IsSuccessStatusCode)
{
//do something
}
}
void uploadFiles()
{
foreach (string filename in filenames)
{
Upload(filename);
}
}
感谢关注我的问题。
答案 0 :(得分:1)
await
上传完成后再上传下一个文件:
async void uploadFiles()
{
foreach (string filename in filenames)
{
await Upload(filename);
}
}