using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers {
  public class ExempleAsyncController : Controller {
    public ActionResult Index() {
      return View();
    }
    private async Task<bool> FaireUnGrosCalculSurUneRessourceAsync( int quelleRessource, int complexite ) {
      return await Task.Run( () => {
        Thread.Sleep( complexite );
        return true; // Retourner void empêcherait de gérer les exceptions avec un simple try / catch
			} );
    }
    private void FaireUnGrosCalculSurUneRessource( int quelleRessource, int complexite ) {
      Thread.Sleep( complexite );
    }
    [HttpPost]
    public async Task<ActionResult> CalculerAsync() {
      ViewBag.DebutTraitement = DateTime.Now;
      var t1 = FaireUnGrosCalculSurUneRessourceAsync( 1, 1200 );
      var t2 = FaireUnGrosCalculSurUneRessourceAsync( 2, 500 );
      var t3 = FaireUnGrosCalculSurUneRessourceAsync( 3, 1100 );
      var t4 = FaireUnGrosCalculSurUneRessourceAsync( 4, 200 );
      await Task.WhenAll(new Task[] { t1, t2, t3, t4 });
      return View( "Index" );
    }
    [HttpPost]
    public ActionResult Calculer() {
      ViewBag.DebutTraitement = DateTime.Now;
      FaireUnGrosCalculSurUneRessource( 1, 1200 );
      FaireUnGrosCalculSurUneRessource( 2, 500 );
      FaireUnGrosCalculSurUneRessource( 3, 1100 );
      FaireUnGrosCalculSurUneRessource( 4, 200 );
      return View( "Index" );
    }
  }
}