Pass parameter in task using lambda

1

I have this afternoon ...

    void TareaFecha()
    {
        Task T = new Task(() =>
        {
            DateTime FechaActual = DateTime.Today;
            DateTime Inicio = FechaActual.AddDays(8);
            MessageBox.Show($"TareaFecha, fecha calculada = {Inicio}");
        });
        T.Start();
    }

I try to pass a whole number as a parameter in the parenthesis on the left side of the lambda; Task T = new Task((dias) => to be able to use it later within the function, in the line; DateTime Inicio = FechaActual.AddDays(dias); and replace it with the constant number 8 but there is no way, with that kind of syntax, where do I assign the value to the parameter?

EDIT:

For example here, I pass the parameter without problem, but, Why?

    void TareaFechaParametro()
    {
        Task T = new Task((dias) => MessageBox.Show($"TareaFecha, fecha calculada = {dias}"), 4);
        T.Start();
    }
    
asked by Edulon 19.10.2017 в 16:54
source

3 answers

1

One possible solution is to create an external method and call it from the task:

void TareaFecha()
{
    Task T = new Task(() => this.SetFecha(8));
    T.Start();
}

void SetFecha(int dias)
{
    DateTime FechaActual = DateTime.Today;
    DateTime Inicio = FechaActual.AddDays(dias);
    MessageBox.Show($"TareaFecha, fecha calculada = {Inicio}");
}

On the other hand, the problem in your code is that you must pass the parameter to the end. This should work:

void TareaFecha(int dia)
{
    Task T = new Task((dias) =>
    {
        DateTime FechaActual = DateTime.Today;
        DateTime Inicio = FechaActual.AddDays((Int32)dias);
        MessageBox.Show($"TareaFecha, fecha calculada = {Inicio}");
    },dia);
    T.Start();
}

If they already had to be more than one parameter, things would get complicated.

    
answered by 19.10.2017 / 17:02
source
2

You can do the following as an example, so you can be guided

//Aquí paso el número con el que deseo trabajar dentro del lambda
Task t = new Task((obj) =>
{
    MessageBox.Show(obj.ToString());
}, 12);
t.Start();

Here I pass 12 as the initial value for the variable obj , of all this the most important thing is that what you pass to the lambda is an Object so if you are going to work with one class or another structure, remember to do the corresponding casting.

I hope it helped you.

    
answered by 19.10.2017 в 17:15
2

One possible solution would be:

Action<int> act = (days) => 
{   
     DateTime FechaActual = DateTime.Today;
     DateTime Inicio = FechaActual.AddDays(days);
     Console.WriteLine("TareaFecha, fecha calculada = "+ Inicio.ToShortDateString());
};

Task.Run(()=> act(2));

That way if you need to pass more parameters you just have to modify your action:

example:

Action<DateTime, int> act = (FechaActual, days) => 
{   
     DateTime Inicio = FechaActual.AddDays(days);
     Console.WriteLine("TareaFecha, fecha calculada = "+ Inicio.ToShortDateString());
};

Task.Run(()=> act(DateTime.Now, 2));
    
answered by 20.10.2017 в 00:59