经典的MVC异步操作方法代码示例

public class PortalController : AsyncController
    {
        private readonly ServiceClient client = new ServiceClient();

        public void AsynchronousAsync()
        {
            ViewData["Title"] = "Parallel asynchronous calls";
            AsyncManager.OutstandingOperations.Increment(3);

            string city = "Seattle";

            client.GetHeadlinesCompleted += (sender, e) =>
            {
                AsyncManager.Parameters["headlines"] = e.Result;
                AsyncManager.OutstandingOperations.Decrement();
            };
            client.GetHeadlinesAsync(city);

            client.GetScoresCompleted += (sender, e) =>
            {
                AsyncManager.Parameters["scores"] = e.Result;
                AsyncManager.OutstandingOperations.Decrement();
            };
            client.GetScoresAsync(city);

            client.GetForecastCompleted += (sender, e) =>
            {
                AsyncManager.Parameters["forecast"] = e.Result;
                AsyncManager.OutstandingOperations.Decrement();
            };
            client.GetForecastAsync(city);
        }

        public ActionResult AsynchronousCompleted(NewsHeadline[] headlines,
            SportsScore[] scores,
            WeatherForecast[] forecast)
        {
            return View("Common", new ViewModel
            {
                Headlines = headlines,
                Scores = scores,
                Forecast = forecast
            });
        }
    }

你可能感兴趣的:(mvc,String,Class,asynchronous,parallel)