简体   繁体   中英

Wait for all Tasks C# MVC

I have a set of images to be rejected or accepted, on the submit of each view page it fires and forgets an API request:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <h1>
        @Html.DisplayFor(modelItem => modelItem.Name)
        <button type="submit" class="btn btn-default" name="isValid" value="true">
            <span class="glyphicon glyphicon-ok"></span>
        </button>
        <button type="submit" class="btn btn-default" name="isValid" value="false">
             <span class="glyphicon glyphicon-remove"></span>
        </button>
    </h1>

    ....
}

and in the controller:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(MultimediaEditView multimediaEditView)
{
    var multimedias = db.Multimedias.Where(x => x.IsValid == null && x.IsActive == true);
    Multimedia multimedia = db.Multimedias.Find(multimediaEditView.ID);
    multimedia.InjectFrom(multimediaEditView);

    if (ModelState.IsValid)
    {
        if (multimedia.IsValid == true)
        {
            if (multimedias.Where(x => x.ProductID == multimedia.ProductID && x.ID != multimedia.ID).Count() != 0)
                multimedia.UploadToExternalApi();
            else
            {
                await multimedia.UploadToExternalApi();
                multimedia.ProcessRelatedFiles();
            }
        }

        db.Entry(multimedia).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
    }

    ...
}

on the last image of the set I need to check all Tasks have finished completing before it sends another API request which groups the images on their server which I am attempting here:

// If not the last image fire and forget
if (multimedias.Where(x => x.ProductID == multimedia.ProductID && x.ID != multimedia.ID).Count() != 0)
    multimedia.UploadToApi();
// else on the last image wait for it to finish then group
else
{
    await multimedia.UploadToApi();
    multimedia.ProcessRelatedFiles();
}

However it looks like it is not waiting for all tasks like I thought it would and there seems to be a lot of functionality to wait for all tasks if you know which tasks are running, is there anything like that when you do not know which tasks have been fired?

Store all tasks in a place that you can access later, for example in a list. Then, you can use the standard await Task.WhenAll . I don't see why you would need to treat the last task specially.

There is no built-in tracking of running tasks and it really can't exist because the framework can't know how you as a developer think about your tasks and their grouping.

I hope you are not trying to wait on tasks that span HTTP requests. That's not generally possible in a reliable way. Need to do something else so that this is not necessary. Don't share state across requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM