using System;
using System.Threading;
using System.Threading.Tasks;
namespace Ch05_UsingCSharp5Dot0.R0502
{
/// <summary>
///
/// </summary>
public class AwaitExpresionLambda
{
/// <summary>
/// Inicio ejecución de la demostración de expresión lambda con operador await.
/// </summary>
public void Ejecutar()
{
Task tarea = ProcesamientoAsincronico();
tarea.Wait();
}
/// <summary>
/// Procesamiento asincrónico usando una expresión lambda y el operador await.
/// </summary>
/// <returns></returns>
private async static Task ProcesamientoAsincronico()
{
// Definición de expresión lambda con uso de operador await:
Func<string, Task<string>> lambdaAsync = async nombre =>
{
await Task.Delay(TimeSpan.FromSeconds(2));
return String.Format("`{0}` se está ejecutando en el ID de thread {1}. "
+ "¿Thread en el pool de threads?: {2}.",
nombre,
Thread.CurrentThread.ManagedThreadId,
Thread.CurrentThread.IsThreadPoolThread);
};
string resultado = await lambdaAsync("Expresión lambda asincrónica");
Console.WriteLine(resultado);
}
public static void Main()
{
new AwaitExpresionLambda().Ejecutar();
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nLlRhc2tzOwoKbmFtZXNwYWNlIENoMDVfVXNpbmdDU2hhcnA1RG90MC5SMDUwMgp7CiAgICAvLy8gPHN1bW1hcnk+CiAgICAvLy8gCiAgICAvLy8gPC9zdW1tYXJ5PgogICAgcHVibGljIGNsYXNzIEF3YWl0RXhwcmVzaW9uTGFtYmRhCiAgICB7CiAgICAgICAgLy8vIDxzdW1tYXJ5PgogICAgICAgIC8vLyBJbmljaW8gZWplY3VjacOzbiBkZSBsYSBkZW1vc3RyYWNpw7NuIGRlIGV4cHJlc2nDs24gbGFtYmRhIGNvbiBvcGVyYWRvciBhd2FpdC4KICAgICAgICAvLy8gPC9zdW1tYXJ5PgogICAgICAgIHB1YmxpYyB2b2lkIEVqZWN1dGFyKCkKICAgICAgICB7CiAgICAgICAgICAgIFRhc2sgdGFyZWEgPSBQcm9jZXNhbWllbnRvQXNpbmNyb25pY28oKTsKCiAgICAgICAgICAgIHRhcmVhLldhaXQoKTsKICAgICAgICB9CgogICAgICAgIC8vLyA8c3VtbWFyeT4KICAgICAgICAvLy8gUHJvY2VzYW1pZW50byBhc2luY3LDs25pY28gdXNhbmRvIHVuYSBleHByZXNpw7NuIGxhbWJkYSB5IGVsIG9wZXJhZG9yIGF3YWl0LgogICAgICAgIC8vLyA8L3N1bW1hcnk+CiAgICAgICAgLy8vIDxyZXR1cm5zPjwvcmV0dXJucz4KICAgICAgICBwcml2YXRlIGFzeW5jIHN0YXRpYyBUYXNrIFByb2Nlc2FtaWVudG9Bc2luY3JvbmljbygpCiAgICAgICAgewogICAgICAgICAgICAvLyBEZWZpbmljacOzbiBkZSBleHByZXNpw7NuIGxhbWJkYSBjb24gdXNvIGRlIG9wZXJhZG9yIGF3YWl0OiAKICAgICAgICAgICAgRnVuYzxzdHJpbmcsIFRhc2s8c3RyaW5nPj4gbGFtYmRhQXN5bmMgPSBhc3luYyBub21icmUgPT4KICAgICAgICAgICAgewogICAgICAgICAgICAgICAgYXdhaXQgVGFzay5EZWxheShUaW1lU3Bhbi5Gcm9tU2Vjb25kcygyKSk7CiAgICAgICAgICAgICAgICByZXR1cm4gU3RyaW5nLkZvcm1hdCgiYHswfWAgc2UgZXN0w6EgZWplY3V0YW5kbyBlbiBlbCBJRCBkZSB0aHJlYWQgezF9LiAiCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICsgIsK/VGhyZWFkIGVuIGVsIHBvb2wgZGUgdGhyZWFkcz86IHsyfS4iLAogICAgICAgICAgICAgICAgbm9tYnJlLAogICAgICAgICAgICAgICAgVGhyZWFkLkN1cnJlbnRUaHJlYWQuTWFuYWdlZFRocmVhZElkLAogICAgICAgICAgICAgICAgVGhyZWFkLkN1cnJlbnRUaHJlYWQuSXNUaHJlYWRQb29sVGhyZWFkKTsKICAgICAgICAgICAgfTsKCiAgICAgICAgICAgIHN0cmluZyByZXN1bHRhZG8gPSBhd2FpdCBsYW1iZGFBc3luYygiRXhwcmVzacOzbiBsYW1iZGEgYXNpbmNyw7NuaWNhIik7CgogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZShyZXN1bHRhZG8pOwogICAgICAgIH0KICAgICAgICAKICAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCiAgICAgICAgewogICAgICAgIAluZXcgQXdhaXRFeHByZXNpb25MYW1iZGEoKS5FamVjdXRhcigpOwogICAgICAgIH0KICAgIH0KfQ==