Vlad
Vlad

Reputation: 878

How to block caller thread after await operator

I'm using new HttpClient class to upload some data to the server, so I was forced to use new operators like await/async. I have following issue: how can I block a caller thread to wait until first file will be uploaded and after that move to the next one.

    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);
        }
    }

Thanks for any attention about my problem.

Upvotes: 0

Views: 94

Answers (1)

dtb
dtb

Reputation: 217313

await that the upload has finished before you upload the next file:

async void uploadFiles()
{
    foreach (string filename in filenames)
    {
        await Upload(filename);
    }
}

Upvotes: 1

Related Questions