Your question is a bit unclear, and I think you have mixed up asynchronous and parallel, which do not necessarily coincide with C #.
There are two types of asynchronization in C # :
-, , , ( ). , .
, , - , Async [YourMethodName], API , TaskCompletionSource . , ( Task.Run - ), ( , , [YourMethodName] Async) , .
, , client.ExecuteAsync
, , ExecuteAsync a Task
.
:
private async Task UploadChunkToServer(UserCustomFile Chunk)
{
var client = new RestClient(Helper.GetServerURL());
var request = new RestRequest("api/fileupload/savechunk", Method.POST);
request.AddHeader("Content-type", "application/json");
request.RequestFormat = RestSharp.DataFormat.Json;
request.AddJsonBody(Chunk);
return await client.ExecuteAsync(request, response =>
{
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
throw new Exception(response.ErrorMessage);
}
else
{
ChunkStatuses.Add(Chunk.ChunkID);
}
});
}
:
if (DataQueue.Count == NumberofChunksToRead)
{
DataQueue.ToList().ForEach(Chunk =>
{
await UploadChunkToServer(Chunk);
});
}
Task, , ..
, Parallel Foreach . , , , -. , , - , , . 4- , 4 , , , . -, .