Пример #1
0
        public static Task<Weather> GetWeatherAsync(string city)
        {
            return Task.Run(
                async () =>
                {
                    await Task.Yield();
                    Console.WriteLine("Starting getting the weather for '{0}'", city);

                    // Faking the temerature by city name length:)

                    // Each task should take random amount of time
                    var interval = 1 + new Random(Guid.NewGuid().GetHashCode()).Next(7);
                    Console.WriteLine("Sleeping for {0}sec", interval);

                    await Task.Delay(TimeSpan.FromSeconds(interval));

                    var result = new Weather(city.Length);
                    Console.WriteLine("Got the weather for '{0}'", city);
                    return result;
                });
        }
Пример #2
0
 private void ProcessWeather(string city, Weather weather)
 {
     Console.WriteLine("[{2}]: Processing weather for '{0}': '{1}'", city, weather,
         DateTime.Now.ToLongTimeString());
 }