简体   繁体   中英

Asp 4 Async Task how to Convert NET 4.5 to NET4

I am trying to transform the following code in NET4.0
How Can I do it ? Please give me soeme help
I only have VS 2010 and cannot change version of NET (stick with 4.0 version)
I have tried to convert the second method as bellow but it doesn't work on

   Task<IEnumerable<IEnumerable<Video>>> GetVideosAsync()
        {
            var allVideosTasks = new List<Task<IEnumerable<Video>>>();
            foreach (var url in sources)
            {
                allVideosTasks.Add(DownloadDataAsync(url));
            }
            var context = TaskScheduler.FromCurrentSynchronizationContext();


            var allVideos = Task.Factory.ContinueWhenAll(
                allVideosTasks.ToArray(), result =>
         {
             int element = result.Count();
             Console.WriteLine("Task={0}, Thread={1} (ContinueWhenAny): number element = {2}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, element);
             var resu = result[0].Result;
         }, CancellationToken.None, TaskContinuationOptions.None, context);

            return (Task<IEnumerable<IEnumerable<Video>>>)allVideos;
        }


/*********     CODE NET4.5 ***********/
   public Task<ActionResult> Async()
    {
     var sw = Stopwatch.StartNew();
     var data = GetVideosAsync();
     sw.Stop();
     ViewBag.Elapsed = sw.ElapsedMilliseconds;
     return View("~/views/home/index.cshtml", data);
    }
    async Task<IEnumerable<IEnumerable<Video>>> GetVideoAsync()
    {
     var allVideos = new List<IEnumerable<Video>>();
     foreach (var url in sources) 
     {
      allVideos.Add(await DownloadAsync(url)); // downloading content from internet 
     }
    }

without re-writing the code completely, you could install the Microsoft.Bcl.Async package through nuget. http://www.nuget.org/packages/Microsoft.Bcl.Async

This package enables Visual Studio 2012 projects to use the new 'async' and 'await' keywords. This package also includes Task-based extension methods that allow using some of the existing asynchronous APIs with the new language keywords.

Supported Platforms:

  • .NET Framework 4 (with KB2468871)

Try the code below for the .NET 4.5 version. In your Async method you need to await the result of GetVideosAsync in order to have the complete list of videos, so method needs to be marked as async as well.

  public async Task<ActionResult> Async()
   {
      var sw = Stopwatch.StartNew();
      var data = await GetVideosAsync();
      sw.Stop();
      ViewBag.Elapsed = sw.ElapsedMilliseconds;
     return View("~/views/home/index.cshtml", data);     
   }

    async Task<IEnumerable<IEnumerable<Video>>> GetVideosAsync()
    {
        var allVideosTasks = new List<Task<IEnumerable<Video>>>();
        foreach (var url in sources)
        {
           allVideosTasks.Add(DownloadDataAsync(url));
        }

        await Task.WhenAll(allVideosTasks.ToArray());       

        return allVideosTasks.Select(x => x.Result);
    }

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